Board index » cppbuilder » padding of characters between types

padding of characters between types


2005-04-25 07:12:11 PM
cppbuilder64
Hi,
afaik when i define structures there will be some 'unusable' spaces between
different types. However recently i found that my original interpretation is
wrong. here is what i found (under all default settings):
struct AA // sizeof = 8, 3 bytes padding between char and int
{
char c;
int i;
};
struct AA // sizeof = 16, 7 bytes padding between char and double
{
char c;
double d;
};
struct AA // sizeof = 16, 3 bytes padding between char and int
{
char c;
int i;
double d;
};
struct AA // sizeof = 16, 4 bytes padding between int and double
{
int i;
double d;
};
at first i think that padding is to pad structures to a multiple of 4 but it
should be wrong. however i cant get the exact rule for this. Could anyone
kindly tell me about this?
Thanks in advance.
 
 

Re:padding of characters between types

"Alex Liu" < XXXX@XXXXX.COM >wrote:
Quote
afaik when i define structures there will be some 'unusable'
spaces between different types. However recently i found that
my original interpretation is wrong. here is what i found
[snip]
Look at Project|Options|Enhanced Comiler Options. There you will
find Alignment: Byte, Word, Double Word and Quad Word.
 

Re:padding of characters between types

"Alex Liu" < XXXX@XXXXX.COM >wrote:
Quote
struct AA // sizeof = 8, 3 bytes padding between char and int
{
char c;
int i;
};
Padding is 3 to get the int onto a 4 byte boundary
Quote
struct AA // sizeof = 16, 7 bytes padding between char and double
{
char c;
double d;
};
Padding is 7 to get the double onto an 8 byte boundary
Quote
struct AA // sizeof = 16, 3 bytes padding between char and int
{
char c;
int i;
double d;
};
Padding is 3 to get the int onto a 4 byte boundary. The double will end
up on a 8 byte boundary anyway
Quote
struct AA // sizeof = 16, 4 bytes padding between int and double
{
int i;
double d;
};
Padding is 4 to get the double onto an 8 byte boundary
Quote
at first i think that padding is to pad structures to a multiple of 4 but it
should be wrong. however i cant get the exact rule for this. Could anyone
kindly tell me about this?
The idea is to try to get the members starting at the boundaries that
they best work at.
The i386 architecture is more efficient loading ints from addresses
which are a multiple of 4 bytes. It's also more efficient loading
doubles from addresses that are a multiple of 8 bytes. That's just the
way the hardware works.
What it means is that, for this architecture, the fastest option is to
assume that the struct (class) is aligned on an 8-byte boundary, and
then the members within the struct at those addresses that allow them to
be loaded fastest.
The interesting effect is that this also applies to array members. So an
array of {double:int} will also attempt to align the address of the
second element's double on an 8-byte boundary, and hence there will be
unused space after the int member.
Alan Bellingham
--
ACCU Conference 2005 - 20-23 April, Randolph Hotel, Oxford, UK
 

{smallsort}