Specifically, this came up in a discussion:
Memory consuption wise, is there a possibility that using a
struct
of twoint
s t
In your specific example, struct S { int a, b; };
, I cannot see any reasonable argument for padding. int
should be naturally aligned already, and if it is, int *
can and should be the natural representation for pointers, and there is no need for S *
to be any different. But in general:
A few rare systems have pointers with different representations, where e.g. int *
is represented as just an integer representing a "word" address, and char *
is a combination of a word address and a byte offset into that word (where the byte offset is stored in otherwise unneeded high bits of the word address). Dereferencing a char *
happens in software by loading the word, and then masking and shifting to get the right byte.
On such implementations, it may make sense to ensure all structure types have a minimal alignment, even if it's not necessary for the structure's members, just so that that byte offset mess isn't necessary for pointers to that structure. Meaning it's reasonable that given struct S { char a, b; };
, sizeof(S) > 2
. Specifically, I'd expect sizeof(S) == sizeof(int)
.
I've never personally worked with such implementations, so I don't know if they do indeed produce such padding. But an implementation that does so would be reasonable, and at the very least very close to an existing real-world implementation.