Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   alignment of structures in c++ (http://www.velocityreviews.com/forums/t450262-alignment-of-structures-in-c.html)

Sandeep 12-04-2005 03:03 PM

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.


Thomas Tutone 12-04-2005 03:24 PM

Re: alignment of structures in c++
 

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


Neelesh Bodas 12-04-2005 03:25 PM

Re: alignment of structures in c++
 
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.


Gianni Mariani 12-04-2005 04:59 PM

Re: alignment of structures in c++
 
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.



Gianni Mariani 12-04-2005 05:18 PM

Re: alignment of structures in c++
 
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.



Thomas Tutone 12-04-2005 09:14 PM

Re: alignment of structures in c++
 

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



All times are GMT. The time now is 03:47 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.