I am using a variable-sized C struct, as follows:
typedef struct {
int num_elems;
int elem1;
} mystruct;
// say I have 5 elements I would like to hold.
mystruct * ms = malloc(sizeof(mystruct) + (5-1) * sizeof(int));
ms->num_elems = 5;
// ... assign 5 elems and use struct
free(ms);
Will this last free() free everything that was malloc’d, or only sizeof(mystruct)?
Yes. This will free the whole block that was allocated using
malloc.If you allocate a single block of memory using
malloc(like you do in your example), then you need to callfreeexactly once to free that entire block.