So say I have a Union
typedef union Union{
int firstField;
char * secondField;
}Union;
and I allocate this union on the heap and allocate secondField on the heap. How do I free the secondField?
So say I have a Union
typedef union Union{
int firstField;
char * secondField;
}Union;
and I allocate this union on the heap and allocate secondField on the heap. How do I free the secondField?
Just in the obvious way:
Union *up;
up = malloc(sizeof(Union));
up->secondField = malloc(n);
// ...
free(up->secondField);
free(up);
Of course, you must not assign to up->firstField in between the assignment and free of up->secondField. So you have to design your code to ensure that this cannot happen.