Let's say I have the following structure:
typedef struct MyObject {
char* tag;
union {
BigObject b; // imagine this is 5MB
SmallObject s; // imagine this is 8 bytes
} u;
}
From the standard (copied from this answer):
...The size of a union is sufficient to contain the largest of its data members. Each data member is allocated as if it were the sole member of a struct.
Is there a way to get the exact size to malloc for a particular struct? It would be something like tag* (8) + padding (?) + [small or big object size] (?). In other words, how can I get what amounts to MyObject o; sizeof(o) which takes into account the proper union-ed object size?