Freeing variable-sized struct in C -
i using variable-sized c struct, follows:
typedef struct { int num_elems; int elem1; } mystruct; // have 5 elements hold. mystruct * ms = malloc(sizeof(mystruct) + (5-1) * sizeof(int)); ms->num_elems = 5; // ... assign 5 elems , use struct free(ms);
will last free() free malloc'd, or sizeof(mystruct)?
yes. free whole block allocated using malloc
.
if allocate single block of memory using malloc
(like in example), need call free
once free entire block.
Comments
Post a Comment