473,387 Members | 1,485 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,387 software developers and data experts.

alignment of structures in c++

struct x
{
int a;
char b;
int c;
char d;
};

typedef struct x X;

int main()
{
printf("Sizeof X:%d\n",sizeof(X));
return 0;
}

output of above : 16 .
Stroustrup says that to optimize space, the members should be ordered
by size ( largest member first).

Why do the compilers not optimize it automatically? In other words,
when would i need the memory layout of the structure to be mainted as
it is declared.

Dec 4 '05 #1
5 1975

Sandeep wrote:
struct x
{
int a;
char b;
int c;
char d;
};

typedef struct x X;

int main()
{
printf("Sizeof X:%d\n",sizeof(X));
return 0;
}

output of above : 16 .
Stroustrup says that to optimize space, the members should be ordered
by size ( largest member first).
What Stroustrup actually says (TCPL 3rd sec. 5.7) is "You can minimize
wasted space by simply ordering members by size (largest member first).
However it is usually best to order members for readability and sort
them by size only if there is a demonstrated need to optimize." In
other words, don't optimize early.
Why do the compilers not optimize it automatically? In other words,
when would i need the memory layout of the structure to be mainted as
it is declared.


Since this a POD struct, it needs to be layout compatible with C. C
requires that the elements appear in the order listed in the
declaration. Letting a C++ compiler rearrange the elements would
therefore break compatibility with C.

Why does C have that requirement? Ask in a C newsgroup.

Best regards,

Tom

Dec 4 '05 #2
Sandeep wrote:
Why do the compilers not optimize it automatically? In other words,
when would i need the memory layout of the structure to be mainted as
it is declared.


Data member variables are initialized in the order in which they are
declared inside the class. If the compiler is allowed to rearrange the
data members in a way it wants, the order of construction (as dictated
by the class writer) would go for a toss - or the compiler would need
to store that somewhere and refer to that every time the object is
instantiated - causing an extra overhead.

Dec 4 '05 #3
Neelesh Bodas wrote:
Sandeep wrote:

Why do the compilers not optimize it automatically? In other words,
when would i need the memory layout of the structure to be mainted as
it is declared.

Data member variables are initialized in the order in which they are
declared inside the class. If the compiler is allowed to rearrange the
data members in a way it wants, the order of construction (as dictated
by the class writer) would go for a toss - or the compiler would need
to store that somewhere and refer to that every time the object is
instantiated - causing an extra overhead.


There is no extra overhead here.

It's simply compliant with the standard.
Dec 4 '05 #4
Thomas Tutone wrote:
.....

Since this a POD struct, it needs to be layout compatible with C. C
requires that the elements appear in the order listed in the
declaration. Letting a C++ compiler rearrange the elements would
therefore break compatibility with C.

Why does C have that requirement? Ask in a C newsgroup.


I think it's topical here (IMHO).

The first C++ compiler (and still many compilers) use a C++ front-end to
a C compiler, so layout rules for data structures were sort of "designed
into" the standard.

C has a "poor man's" type of inheritance. If the compiler re-ordered
struct members it would make it almost impossible to do that. IIRC,
alignment padding was not an issue in the first incantations of the C
compiler. The first C compiler I used I don't believe did any alignment
padding. So to maintain compatability with code written on systems
where alignment padding was not needed, a rule like the one currently in
the standard had to be enforced, otherwise a whole bunch of code would
break.

So, nothing stops you from ordering the members in your struct in a
better way. You may be constrained however.

e.g. (A made-up example use your imagination).

struct A
{
A( B * foo, C * zoo )
: m_btype( B->ComputeBtype( zoo ) ),
m_xtype( new X( m_btype ) ),
m_ytype( new Y( zoo->Fetch( m_btype ) )
{
}

char m_btype;
X * m_xtype;
Y * m_ytype;
char m_code[3];
};

In this case, if you move m_btype, your're introducing a bug.
Dec 4 '05 #5

Gianni Mariani wrote:
Thomas Tutone wrote:
....

Since this a POD struct, it needs to be layout compatible with C. C
requires that the elements appear in the order listed in the
declaration. Letting a C++ compiler rearrange the elements would
therefore break compatibility with C.

Why does C have that requirement? Ask in a C newsgroup.


I think it's topical here (IMHO).


Apologies - I didn't mean to imply otherwise - far be it from me to act
as the OT police. I did think that since it was a C question, the OP
might get more knowledgeable answers in a C newsgroup.

Best regards,

Tom

Dec 4 '05 #6

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

Similar topics

36
by: Bhalchandra Thatte | last post by:
I am allocating a block of memory using malloc. I want to use it to store a "header" structure followed by structs in my application. How to calculate the alignment without making any assumption...
4
by: Shashi | last post by:
Can somebody explain how the byte alignment for structures work, taking the following example and considering: byte of 1 Byte word of 2 Bytes dword of 4 Bytes typedef struct { byte a; word...
10
by: j0mbolar | last post by:
for any pointer to T, does a pointer to T have different or can have different alignment requirement than a pointer to pointer to T? if so, where is the exact wording in the standard that would...
67
by: S.Tobias | last post by:
I would like to check if I understand the following excerpt correctly: 6.2.5#26 (Types): All pointers to structure types shall have the same representation and alignment requirements as each...
14
by: gamja | last post by:
Hi all. This is my first post on this group. Nice to meet you, cool guys~! I'm on system programming on various embedded systems and understand very well the byte alignment issues. When I write...
10
by: haomiao | last post by:
I want to implement a common list that can cantain any type of data, so I declare the list as (briefly) --------------------------------------- struct list { int data_size; int node_num;...
33
by: silpau | last post by:
struct a { int b; char a; int c; } On i386 the sizeof this structure would be 12 bytes,as 3 bytes are padded after a so that c is aligned on 4byte boundary.
55
by: fishpond | last post by:
How to declare a variable guaranteed to have the strictest possible alignment? -- The defense attorney was hammering away at the plaintiff: "You claim," he jeered, "that my client came at you...
5
by: xmllmx | last post by:
Please forgive me for cross-posting. I've post this to microsoft.publoc.vc.mfc. But I can't get any response. Maybe only MFC- related topics are cared there. To begin with code: union XXX {...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.