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

Popular posts from this blog

c++ - How do I get a multi line tooltip in MFC -

asp.net - In javascript how to find the height and width -

c# - DataTable to EnumerableRowCollection -