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

windows - Why does Vista not allow creation of shortcuts to "Programs" on a NonAdmin account? Not supposed to install apps from NonAdmin account? -

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

unit testing - How to mock PreferenceManager in Android? -