i was playing arround with the C malloc and free tools and i had a weird memory leak. Does someone has an idea about it?
The goal is to successfully free a char**.
So in the function freezer, i free every char* in the char** and the i free the char**.
But Valgrind (my leaks detector on linux) find 20 bytes in 4 blocks leaked (i don't know if i can write 'leaked' XD) The more interesting part is that if i do a bigger char** by adding a char* in it, it leak 5 more bytes of memory in another block :/.
#include <stdio.h>
#include <stdlib.h>
void freezer(char ***array, int length){
int i;
i = -1;
while (*array[++i] != NULL){
free(*array[i]);
}
free(*array);
}
int main(){
char **big;
int len = 4;
int i;
big = malloc(sizeof(char *) * (len + 1));
i = -1;
while (++i < len){
big[i] = malloc(sizeof(char) * 5);
big[i][0] = 't';
big[i][1] = 'e';
big[i][2] = 's';
big[i][3] = 't';
big[i][4] = '\0';
}
big[i] = NULL;
i = -1;
while (++i < len){
printf("i: %d\t%s\n", i, big[i]);
}
freezer(&big, len);
return (0);
}
You can directly copy/past/run the code as it is.
So if you have any clue about the error/C problem, please let me know.