Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > struct memory padding

Reply
Thread Tools

struct memory padding

 
 
simonp@nospam.com
Guest
Posts: n/a
 
      01-22-2007
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

--
Stupendous Tales
www.stupendoustales.com
Speculative Fiction, Pulp Dreams
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      01-22-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> 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.

--
Ian Collins.
 
Reply With Quote
 
 
 
 
Yahooooooooo
Guest
Posts: n/a
 
      01-22-2007
Hope this helps you
http://www.thescripts.com/forum/thread543879.html


(E-Mail Removed) wrote:
> 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
>
> --
> Stupendous Tales
> www.stupendoustales.com
> Speculative Fiction, Pulp Dreams


 
Reply With Quote
 
simonp@nospam.com
Guest
Posts: n/a
 
      01-22-2007
Thanks so much. Padding of the struct will bring it up to the
next multiple of the word size, eg. 32bit/4byte

Cheers,
Simon

--
Stupendous Tales
www.stupendoustales.com
Speculative Fiction, Pulp Dreams
 
Reply With Quote
 
Gianni Mariani
Guest
Posts: n/a
 
      01-22-2007
(E-Mail Removed) wrote:
> 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.
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      01-22-2007
Yahooooooooo wrote:
> Hope this helps you
> http://www.thescripts.com/forum/thread543879.html
>
>

Please don't top-post.

--
Ian Collins.
 
Reply With Quote
 
John Carson
Guest
Posts: n/a
 
      01-22-2007
<(E-Mail Removed)> wrote in message news:ep1ok2$h9a$(E-Mail Removed)
> 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.

--
John Carson


 
Reply With Quote
 
Ivan Vecerina
Guest
Posts: n/a
 
      01-22-2007
<(E-Mail Removed)> wrote in message
news:ep1ok2$h9a$(E-Mail Removed)...
: 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

 
Reply With Quote
 
Greg
Guest
Posts: n/a
 
      01-22-2007
(E-Mail Removed) wrote:
> 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

 
Reply With Quote
 
peter koch
Guest
Posts: n/a
 
      01-22-2007

(E-Mail Removed) wrote:
> Thanks so much. Padding of the struct will bring it up to the
> next multiple of the word size, eg. 32bit/4byte
>
> Cheers,
> Simon
>
> --
> Stupendous Tales
> www.stupendoustales.com
> Speculative Fiction, Pulp Dreams

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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can *common* struct-members of 2 different struct-types, that are thesame for the first common members, be accessed via pointer cast to either struct-type? John Reye C Programming 28 05-08-2012 12:24 AM
struct padding ??? vikas talwar C Programming 3 05-31-2008 10:06 PM
Padding bits and struct assignment Hallvard B Furuseth C Programming 5 12-27-2006 10:19 PM
struct padding edware C Programming 9 04-27-2006 12:40 AM
struct my_struct *p = (struct my_struct *)malloc(sizeof(struct my_struct)); Chris Fogelklou C Programming 36 04-20-2004 08:27 AM



Advertisments