user9477548
user9477548

Reputation: 97

Memory leak. Trouble freeing malloc'ed char*

This program compares two strings stored in dynamic memory.

The Destroy function should free all malloc'ed space.

When using Valgrind, it shows that there is a memory leak in my program that is the exact size of the malloc'ed data that stores the strings being compared.

I am not sure what I am doing wrong.

I was told that pMy_string->data = c_string; is the problem but I don't know why or how to fix this.

I tried to submit the rest of my code but stack overflow kept saying that I included too much code and would not allow me to submit my question so all that I was able to include were the following functions. I hope this is enough.

/* 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;
}

Upvotes: 1

Views: 512

Answers (1)

alk
alk

Reputation: 70931

I was told that pMy_string->data = c_string; is the problem but I don't know why or how to fix this.

In C

  • a string in fact is an array of char, holding (at least) '\0' character.
  • arrays cannot be assigned.

So instead of doing

pMy_string->data = c_string;

copy the array's elements until the '\0' is reached:

strcpy(pMy_string->data, c_string); /* The prototype to strcpy() is in string.h. */

Upvotes: 2

Related Questions