Эта программа сравнивает две строки, хранящиеся в динамической памяти.
Функция Destroy должна освободить все пространство, выделенное malloc.
При использовании Valgrind в моей программе обнаруживается утечка памяти, которая является точным размером данных malloc, которыехранит сравниваемые строки
Я не уверен, что я делаю неправильно.
Мне сказали, что pMy_string->data = c_string;
- это проблема, но я не знаю, почему или как это исправить.
Я пытался отправить оставшуюся часть кода, но переполнение стека продолжало говорить, чтоЯ включил слишком много кода и не позволил мне представить свой вопрос, поэтому все, что я мог включить, были следующие функции.Надеюсь, этого достаточно.
/* my_string.c */
#include <stdlib.h>
#include <stdio.h>
#include "my_string.h"
/*** STRUCTS ***/
/* define my_string object */
struct my_string {
int size;
int capacity;
char* data;
};
typedef struct my_string My_string;
/*** HELPER FUNCTIONS ***/
/* Counts the length of a given string and ends at the
null byte. */
int count_my_string( const char* c_string ){
int i = 0, count = 0;
while( c_string[i] != '\0'){
i += 1;
count += 1;
}
return count;
}
/* Allocate memory to hold a string of characters. */
/* Returns a pointer to the first byte of Dynamic
Memory. */
MY_STRING my_string_dynamic_mem( int my_string_size, int my_string_capacity ){
My_string* pMy_string = NULL;
pMy_string = ( My_string* )malloc( sizeof( My_string ));
if ( pMy_string != NULL ){
pMy_string->size = my_string_size;
pMy_string->capacity = my_string_capacity;
pMy_string->data = ( char* )malloc( pMy_string->capacity * sizeof( char ));
if ( pMy_string->data == NULL ){
free( pMy_string );
pMy_string = NULL;
}
}
else{
printf( "ERROR: Unable to dynamically allocate memory.\n" );
free( pMy_string );
exit( 1 );
}
return pMy_string;
}
/* INITIALIZIERS */
/* default init */
MY_STRING my_string_init_default( void ){
My_string* pMy_string = NULL;
pMy_string = my_string_dynamic_mem( 0, 7 );
return pMy_string;
}
/* init for predetermined c-string. */
MY_STRING my_string_init_c_string( char* c_string ){
My_string* pMy_string = NULL;
int my_string_size = 0;
int my_string_capacity = 0;
my_string_size = count_my_string( c_string );
my_string_capacity = ( my_string_size + 1 );
pMy_string = my_string_dynamic_mem( my_string_size, my_string_capacity );
pMy_string->data = c_string;
return pMy_string;
}
/*** DESTROY ***/
/* Free dynamically allocated memory. */
void my_string_destroy( MY_STRING* phMy_string ){
My_string* pMy_string = ( My_string* )*phMy_string;
/*
printf("\nThe pMy_string->data pointer is: %p\n",&pMy_string->data );
printf("The pMy_string pointer is: %p\n",&pMy_string );
*/
pMy_string->data = NULL;
free( pMy_string->data );
free( pMy_string );
*phMy_string = NULL;
return;
}
/*** GETTERS ***/
int my_string_get_capacity( MY_STRING hMy_string ){
My_string* pMy_string = ( My_string* )hMy_string;
return pMy_string->capacity;
}
int my_string_get_size( MY_STRING hMy_string ){
My_string* pMy_string = ( My_string* )hMy_string;
return pMy_string->size;
}
char* my_string_get_data( MY_STRING hMy_string ){
My_string* pMy_string = ( My_string* )hMy_string;
return pMy_string->data;
}