E
Eric Sosman
Understood. How about below:[... a question about the size and padding of a struct
"defined" by uncompilable code ...]
I have not given a compilable code. I am just
asking the size of the struct given.
How big is this array:
int array<7>;
? In other words, if the code describing your struct won't even
compile, then you have not "given" a struct at all. If there is
no struct, it has no size and no padding -- and no existence.
int main(void) {
struct test {
char a;
int b;
short c;
};
*Much* better!
printf("%d\n", sizeof(struct test));
return 0;
}
I completely understand what will be the size of the struct with and
without mapping but the question is what parameters decides the padding
involved? Such as size of registers or size of address/data bus of the
processor?
There are different answers at different levels.
At the hardware level, alignment and the padding to attain
it are artifacts of the memory subsystem: Not just the busses,
but the address translation circuitry, the various levels of
cache, the inter-processor data-consistency protocols, and so
on. This collection of components may find it easier or cheaper
or faster to access particular types on a restricted set of
addresses: For example, it might be advantageous to position a
`double' object on an eight-byte boundary or an `int' on a
four-byte boundary. (I don't think register widths have much
to do with this -- but I'm no hardware designer, so don't treat
my "I don't think" as Gospel.)
But that's not the whole story. A host seldom consists only
of the hardware; there's an operating system to think about. The
O/S usually specifies an "application binary interface" or ABI
that describes how data should be arranged when invoking system
services or when dissecting their results. For example, even if
the hardware is able to cope with an `int' at an arbitrary address,
the ABI might insist on four-byte alignment (one possible reason
to do so could be to simplify the "Does the caller really have
access to all the bytes implied by this pointer?" test, by not
having to worry about crossing page boundaries). Then again, an
ABI might choose *not* to cater to all the hardware's whims: For
example, a widely-used ABI calls for four-byte alignment of all
stack-allocated data, even `double' objects that would be
significantly faster if allocated on eight-byte boundaries.
But even that isn't the whole story. Eventually, it's the
developers of the compiler itself who decide what policies it will
enforce. One developer might say "Speed is important: We'll put
every object on an address that makes accesses the very fastest
they can possibly be." Another might say "Memory bloat should be
avoided: We'll pack the objects as tightly as we can while still
maintaining reasonable (not optimal) speed." Yet another might
say "This is an embedded machine with only 4KB of RAM, so memory
is an extremely scarce resource and we'll pack everything down to
the absolute minimum." In the end, that is, it's a human choice.