473,320 Members | 2,097 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

struct memory padding

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
Jan 22 '07 #1
11 3601
si****@nospam.com 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.
Jan 22 '07 #2
Hope this helps you
http://www.thescripts.com/forum/thread543879.html
si****@nospam.com 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
Jan 22 '07 #3
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
Jan 22 '07 #4
Yahooooooooo wrote:
Hope this helps you
http://www.thescripts.com/forum/thread543879.html

Please don't top-post.

--
Ian Collins.
Jan 22 '07 #5
si****@nospam.com 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.
Jan 22 '07 #6
<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?
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
Jan 22 '07 #7
<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

Jan 22 '07 #8
si****@nospam.com 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

Jan 22 '07 #9

si****@nospam.com 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

Jan 22 '07 #10

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

Jan 22 '07 #11
peter koch <pe***************@gmail.comwrote:
>
si****@nospam.com 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
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

--
Stupendous Tales
www.stupendoustales.com
Speculative Fiction, Pulp Dreams
Jan 22 '07 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Roy Hills | last post by:
When I'm reading from or writing to a network socket, I want to use a struct to represent the structured data, but must use an unsigned char buffer for the call to sendto() or recvfrom(). I have...
5
by: Kobu | last post by:
In embedded systems (programmed in C), often times structure declarations are used to group together several status/control/data registers of external hardware (or even internal registers). The...
21
by: hermes_917 | last post by:
I want to use memcpy to copy the contents of one struct to another which is a superset of the original struct (the second struct has extra members at the end). I wrote a small program to test...
5
by: Hallvard B Furuseth | last post by:
Does struct assignment copy padding bytes? Some compilers do, but I couldn't find anything in the standard which says they must. What I need is for any padding bytes to contan initialized values...
8
by: =?Utf-8?B?R2Vvcmdl?= | last post by:
Hello everyone, I heard when define a struct, it is important to make the size of the struct memory aligned. For example, struct foo
14
by: barcaroller | last post by:
I have a multi-field struct and a memory block (created using malloc) that contains structured data. If I map the struct to the memory block, am I guaranteed that the struct fields will be filled...
19
by: rmr531 | last post by:
First of all I am very new to c++ so please bear with me. I am trying to create a program that keeps an inventory of items. I am trying to use a struct to store a product name, purchase price,...
6
by: Francois Grieu | last post by:
Hello, I'm asking myself all kind of questions on allocating an array of struct with proper alignment. Is the following code oorrect ? I'm most interested by the statement t =...
3
by: vikas talwar | last post by:
Hi All, Can you please explain me how the 'C' compiler allocate memory to 'struct'. Please go thu the example below and pls suggest me the solution for my problem. Here is my structure...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.