I need your advice on this piece of code: the table fields options[0], options[1] etc... doesn't seem to be freed correctly. Thanks for your answers
int main()
{
....
char **options;
options = generate_fields(user_input);
for(i = 0; i < sizeof(options) / sizeof(options[0]); i++) {
free(options[i]);
options[i] = NULL;
}
free(options);
}
char ** generate_fields(char *)
{
char ** options = malloc(256*sizeof(char *));
...
return options;
}
The problem is this:
for(i = 0; i < sizeof(options) / sizeof(options[0]); i++)
options
is a pointer type, not an array type, so sizeof(options)
will always be the same (typically 4 bytes on a 32-bit machine or 8 bytes on a 64-bit machine), so sizeof(options)/sizeof(options[0])
will almost always be 1.
The key is to always free
memory in the same manner as you malloc
'ed it. So, if you malloc
a 2-dimensional array and then malloc
a series of 1-dimensional arrays, you need to do the reverse when freeing it:
char ** generate_fields(char *)
{
char ** options = malloc(256*sizeof(char *));
for(int i = 0; i < 256; i++)
options[i] = malloc(some_size);
return options;
}
void free_fields(char ** options)
{
for(int i = 0; i < 256; i++)
free(options[i]);
free(options);
}
Note that if the size (256 in this case) is not a constant, you need to keep track of it yourself, since otherwise you have no way of knowing how many times to loop when freeing.
You should have the same number of free
s as you have malloc
s.
In your code you allocate the array of pointers, but you don't allocate any memory for the individual elements of the array to point to. But your freeing code is written as if you did.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With