C release dynamically allocated memory

Posted by user1152463 on Stack Overflow See other posts from Stack Overflow or by user1152463
Published on 2013-11-10T21:37:17Z Indexed on 2013/11/10 21:54 UTC
Read the original article Hit count: 214

Filed under:
|
|
|

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;

© Stack Overflow or respective owner

Related posts about c

    Related posts about memory-management