I don’t understand how to free() the strings in my array char**
When I try to do it in this code I got a segfault and 40 bytes lost with valgrind
int main(void)
{
char **strs;
char *ret;
char *sep = "...";
int size = 4;
int i = 0;
strs = malloc(sizeof(char *) * 4);
while (i < 4)
{
strs[i] = malloc(sizeof(char) * 10);
i++;
}
strs[0] = "Why";
strs[1] = "it";
strs[2] = "doesn't";
strs[3] = "work";
ret = foofoo(size, strs, sep);
printf("%sn", ret);
i = 0;
while (i < size)
{
free(strs[i]);
i++;
}
free(strs);
free(ret);
return (0);
}
If I remove this part, no error will occur but if I compile with gcc -g and execute with valgrind I definitely lost 40 bytes because i don’t free() the strings.
while (i < size)
{
free(strs[i]);
i++;
}
Can you explain me please.
Thx
New contributor
Plaket is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.