C release dynamically allocated memory
- by user1152463
I have defined function, which returns multidimensional array.
allocation for rows
arr = (char **)malloc(size);
allocation for columns (in loop)
arr[i] = (char *)malloc(v);
and returning type is char**
Everything works fine, except freeing the memory. If I call free(arr[i]) and/or free(arr) on array returned by function, it crashes.
Thanks for help
EDIT::
allocating fuction
pole = malloc(zaznamov);
char ulica[52], t[52], datum[10];
float dan;
int i = 0, v;
*max = 0;
while (!is_eof(f))
{
get_record(t, ulica, &dan, datum, f);
v = strlen(ulica) - 1;
pole[i] = malloc(v);
strcpy(pole[i], ulica);
pole[i][v] = '\0';
if (v > *max)
{
*max = v;
}
i++;
}
return pole;`
part of main where i am calling function
pole = function();
releasing memory
int i;
for (i = 0; i < zaznamov; i++)
{
free(pole[i]);
pole[i] = NULL;
}
free(pole);
pole = NULL;