struct memory padding

S

simonp

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?

Cheers,
Simon
 
I

Ian Collins

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?
While not defined by the standard, padding tends to be to the natural
alignment of the processor So on a 32 bit processor, the struct

struct X {
char a;
int b;
};

will have a size of 8 with three padding bytes after 'a'.

Some platforms support packing to eliminate padding, but this is
non-standard and impossible on some processors.
 
S

simonp

Thanks so much. Padding of the struct will bring it up to the
next multiple of the word size, eg. 32bit/4byte

Cheers,
Simon
 
G

Gianni Mariani

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?

While padding is specifically allowed in the
C++ standard, it is not specified. It in implementation dependant.

In general, however, the implementations of padding is fairly uniform.

Each member of a struct has an "alignment" which is implementation
defined. The alignment of various types is sometimes mandatory (like in
RISC processors - MIPS, PowerPC, SPARC) and sometimes and optimization
(e.g. IA32, AMD64).

The resulting alignment of a struct is the alignment of the member(s)
with the largest alignment.

e.g.

struct X { char a; char b; }; // has alignment of char

struct Y { char a; int b; short c; }; // has alignment of int

Padding is added between members to guarentee that the offset of all
members in the struct falls on a multiple of the alignment.

e.g.
if alignment of :

char is 1
long long is 8

then

struct Z
{
char a;
// padding added here (7 bytes)
long long b; // offset is 8
};

sizeof Z is 16
alignment of Z is 8

.....

While that is the basic implementation, the actual alignments for each
type varies between platforms and sometimes on different "architectures"
or ABI's on each platform. For example, on MIPS o32, alignment of
double is 4 while on MIPS n32, alignment of double is 8 (IIRC).

It's really not all that hard.
 
J

John Carson

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?

The C++ standard makes no rules on this. It depends on the platform and is
sometimes controllable using compiler switches. It can also vary with the
order of declaration of variables.

Using VC++ on Windows, you can eliminate all padding if you want. By
default, a struct is rounded up to a multiple of its largest data member,
but still leaves a possible role for the order of declaration, e.g.,

struct X1
{
char ch1;
int x1;
char ch2;
int x2;
};

has a size of 16. However, the following struct, with the same members in a
different order, has a size of 12.

struct X2
{
char ch1;
char ch2;
int x1;
int x2;
};

The reason that X1 is larger is because the two ints are aligned on 4 byte
boundaries, so there are 3 bytes after each char. With X2, by contrast, you
only need 2 bytes after ch2 to achieve alignment of the ints on 4 byte
boundaries.

Read up the documentation for the compiler you are interested in to see how
it is handled.
 
I

Ivan Vecerina

: 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
 
G

Greg

Thanks so much. Padding of the struct will bring it up to the
next multiple of the word size, eg. 32bit/4byte

There is no standard rule that will predict how a C++ compiler will
distribute padding bytes (if any) within a particular struct.

In fact, it would be a mistake to think that there is only alignment
convention per machine architecture. On the contrary, alignment
conventions tend to accumulate over time. Old ones never really go away
- because as soon as some struct with a particular alignment convention
is saved to a disk for the first time - then that alignment convention
will effectively be around forever. For example, C++ compilers on the
Macintosh recognized three different alignment conventions that exist
on that platform (68K, Power, "natural"). And most C++ compilers allow
a programmer to specify the size and location of a struct's padding
bytes themselves, just in case the conventional schemes are not enough.

Greg
 
P

peter koch

Thanks so much. Padding of the struct will bring it up to the
next multiple of the word size, eg. 32bit/4byte

Cheers,
Simon
The statements in the link given in a previous post were not entirely
accurate. Basically, padding occurs because the underlying CPU either
requires or prefers a given alignment when reading/writing fundamental
types. This is not necesarrily the same size as the word size. One
example is the newer x86 processors where an optimal reading of doubles
requires the double to be aligned on an 8 byte boundary. Thus
struct s { char c; double d; } will most likely occupy 16 bytes.

/Peter
 
P

peter koch

peter koch wrote:
[snip]
The statements in the link given in a previous post were not entirely
accurate.
[snip]
And some very good explanations have been given elsewhere in this
thread.

/Peter
 
S

simonp

peter koch said:
The statements in the link given in a previous post were not entirely
accurate. Basically, padding occurs because the underlying CPU either
requires or prefers a given alignment when reading/writing fundamental
types. This is not necesarrily the same size as the word size. One
example is the newer x86 processors where an optimal reading of doubles
requires the double to be aligned on an 8 byte boundary. Thus
struct s { char c; double d; } will most likely occupy 16 bytes.

/Peter

My teacher has given me no background such as this.

This is the 2nd course in the 2 course intro to programming. The
1st course used to be C++, but now it is Java, so the low level
programming techniques that used to be covered in the 1st course
are lost. But the teacher has not adjusted the 2nd course
appropriately. Maddening. Though I guess its always better to
learn on your own.

Cheers
Simon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top