So. I'm going to make some assumptions.
One of these is that all pointers have the same size. I'm also going to assume that you're on a 64 bit platform, which means that all of your pointers are probably 8 bytes long.
Additionally, I'll assume an int
is the same as int32_t
. And a short
is an int16_t
. And that char
is int8_t
, and that there are 8 bits in a byte.
typedef struct rubble
{
int betty; // offset = 0 size = 4
char barney[4]; // offset = 4 size = 4
struct rubble* bambam; // offset = 8 size = 8
} rubble;
So the total size here is likely going to be 16.
typedef struct{
short* wilma[2]; // offset = 0 size = 16
short fred[2]; // offset = 16 size = 4
rubble dino; // offset = 24 size = 16
} flinstone;
For a total size of 40.
Now, note that dino
starts further into flinstone
then you might imagine. This is because rubble
needs 8 byte alignment.
Now, it's also possible that the alignment needs are different for different platforms, and the types could have different sizes as well. But this is a possible outcome.
It's also possible that structs will have padding at the end of them. For example:
struct x {
uint32_t a; // offset = 0 size = 4
uint8_t b; // offset = 4 size = 1
};
The total size of x
is 8, because we need to support the case where we have an array of x
, and keep x[2].a
aligned.