<si****@nospam.comwrote in message
news:ep**********@reader2.panix.com...
: I'm taking an intro course on C++, and our teacher is not being
: clear on how stuct memory padding is determined.
:
: If the memory used by all components defined inside a struct
: falls between a certain gradient, a struct "rounds up" to the
: nearest multiple of the gradient.
:
: This teacher is somewhat erratic and has said on subsequent days
: that first the padding is rounded up to nearest multiple of 4,
: and then the nearest power of 2.
:
: I reading on my own, it looks like the padding of the struct is
: determined by the word size fo the platform, which I assume means
: multiples of 16.
:
: Could someone well-versed in C++ please point out how this is
: done?
[ keep in mind that everything related to padding is architecture-
dependent, and not something specified in the C++ standard ]
Padding is dependent on the alignment requirements of the data
members (or fields) of the struct.
For example:
struct FiveChars { char a,b,c,d,e; };
often does not have any padding (neither between members,
nor at the end of the struct): sizeof(FiveChars)==5
Here's how I would summarize things:
The alignment requirement of a data member normally is a
power of two, usually up to the largest word size of the
target architecture.
For example: char<->1 short<->2 long<->4 long long<-4 or 8 ?
(but on a 16-bit architecture, 2 bytes might be the largest alignment
requirement, applying to all primitive types except (signed/u)char ).
Each data member is preceded by the padding required to align
itself properly relative to the beginning of the struct.
The size of the whole struct is padded so that it is a multiple
of the largest alignment requirement of its data fields.
I hope this helps --Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form