If I have a struct:
struct Rec
{
uint16_t vals[500];
};
Rec * prec = malloc(sizeof(Rec));
//Rec * prec = (Rec *) malloc(sizeof(Rec)); This code was my original and is incorrect.
// See Below for details.
// initialize everything in vals
Will this code suffice to free all memory used?
free(prec);
Or do I have to free the array separately?
This will suffice.
You did not allocate the array separately, so just freeing the allocated pointer shall suffice.
Follow the Rule:
You should only call
freeon address returned to you bymalloc, anything else will result in Undefined Behavior.References:
c99 Standard: 7.20.3.2 The
freefunction