alignment of structures in c++

S

Sandeep

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.
 
T

Thomas Tutone

Sandeep said:
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
 
N

Neelesh Bodas

Sandeep said:
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.
 
G

Gianni Mariani

Neelesh said:
Sandeep wrote:




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

Gianni Mariani

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.
 
T

Thomas Tutone

Gianni said:
Thomas Tutone wrote:
....

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
 

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,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top