views:

198

answers:

5

Is the way C++ structs are laid out set by the standard, or at least common across compilers?

I have a struct where one of its members needs to be aligned on 16 byte boundaries, and this would be easier if I can guarantee the ordering of the fields.

Also, for non-virtual classes, is the address of the first element also likely to be the address of the struct?

I'm most interested in GCC and MSVS.

+6  A: 

C++ inherits from c the need/desire to work efficiently on many platforms, and therefore leaves certain things up to the compiler. Aside from requiring elements to appear in the specified order, this is one of them.

But many compilers support #pragmas and options to let you establish come control of the packing. See your compiler's documentation.

dmckee
But what about ABI requirements? Surely compilers that use the same ABI should have the same object layout. It's not a part of the standard, but compilers usually conform to some ABI standard.
Alex B
@Alex: I imagine that depends on how exacting the ABI is. Even then you need to the option to break the rules for interoperability with *other* systems.
dmckee
+4  A: 

Is the way C++ structs are laid out set by the standard, or at least common across compilers?

There is no guarantee in the standard. I would not depend on compilers having the same alignment

I have a struct where one of its members needs to be aligned on 16 byte boundaries, and this would be easier if I can guarantee the ordering of the fields.

Compilers will not change the order of the fields.

Here are some links on how to set them for both GCC and MSVC:
For GCC: http://developer.apple.com/mac/library/documentation/DeveloperTools/gcc-4.0.1/gcc/Structure_002dPacking-Pragmas.html
MSVC: http://msdn.microsoft.com/en-us/library/ms253935(VS.80).aspx and http://msdn.microsoft.com/en-us/library/2e70t5y1(VS.80).aspx

I would keep them as structures and use an extern "C" to ensure that it works properly. Maybe not needed but that would definitely work.

Romain Hippeau
A: 

Structures are laid out sequentially in memory. However, the way they're aligned in memory varies based on the operating system.

For things bigger than 4 bytes, there's a difference between Windows and Linux. Linux aligns them as if they're 4 bytes, so for example a double (8 bytes) could start at p+4, p+8, p+12, etc. where p is the start of the structure. In Windows, a double (8 bytes) needs to start at an address that's a multiple of 8 so p+8, p+16, p+24, etc.

Alex Zylman
That's not determined by the operating system, but mostly by the CPU architecture and the compiler.
wallyk
Alex Zylman
Its a little bit of both. The hardware puts a minimum requirement on everything, but you can always choose to be more strict. If MS chose to do so, then gcc on Windows would have no choice but to follow suit.
Dennis Zickefoose
+2  A: 

C structs (and C++ PODs, for compatibility) are required by the standard to have sequential layout.

The only difference between compilers is alignment, but fortunately, both MSVC and GCC support #pragma pack.

dan04
@Dan04: see my answer -- while the committee certainly *intended* for PODs to have this requirement, a weird corner case was overlooked, so the requirement doesn't apply to quite all POD types.
Jerry Coffin
@jerry: It would be useful if you could provide some kind of reference to this "weird corner case" for the benefit of the rest of us.
Arafangion
@Arafangion: See my edited answer.
Jerry Coffin
@Jerry: Awesome, upvoted that enlightening answer. :)
Arafangion
+7  A: 

C and C++ both guarantee that fields will be laid out in memory in the same order as you define them. For C++ that's only guaranteed for a POD type1 (anything that would be legitimate as a C struct [Edit: C89/90 -- not, for example, a C99 VLA] will also qualify as a POD).

The compiler is free to insert padding between members and/or at the end of the struct. Most compilers give you some way to control that (e.g., #pragma pack(N)), but it does vary between compilers.

1Well, there is one corner case they didn't think of, where it isn't guaranteed for a POD type -- an access specifier breaks the ordering guarantee:

struct x { 
    int x;
    int y;
public:
    int z;
};

This is a POD type, but the public: between y and z means they could theoretically be re-ordered. I'm pretty sure this is purely theoretical though -- I don't know of any compiler that does reorder the members in this situation (and unless memory fails me even worse than usual today, this is fixed in C++0x).

Edit: the relevant parts of the standard (at least most of them) are §9/4:

A POD-struct is an aggregate class that has no non-volatile data members of type pointer to member, non-POD-struct, non- POD-union (or array of such types) or reference, and has no user-defined copy assignment operator and no user-defined destructor.

and §8.5.1/1:

An aggregate is an array or a class (clause 9) with no user- declared constructors (12.1), no private or protected non- static data members (clause 11), no base classes (clause 10) and no virtual functions (10.3).

and §9.2/12:

...the order of allocation of non-static data members separated by an access-specifier is unspecified (11.1).

Though that's restricted somewhat by §9.2/17:

A pointer to a POD-struct object, suitably converted using a reinterpret_cast, points to its initial member...

Therefore, (even if preceded by a public:, the first member you define must come first in memory. Other members separated by public: specifiers could theoretically be rearranged.

I should also point out that there's some room for argument about this. In particular, there's also a rule in §9.2/14:

Two POD-struct (clause 9) types are layout-compatible if they have the same number of nonstatic data members, and corresponding nonstatic data members (in order) have layout-compatible types (3.9).

Therefore, if you have something like:

struct A { 
    int x;
public:
    int y;
public:
    int z;
};

It is required to be layout compatible with:

struct B {
    int x;
    int y;
    int z;
};

I'm pretty sure this is/was intended to mean that the members of the two structs must be laid out the same way in memory. Since the second one clearly can't have its members rearranged, the first one shouldn't be either. Unfortunately, the standard never really defines what "layout compatible" means, rendering the argument rather weak at best.

Jerry Coffin
The reordering bit seems troubling. I'd love to have a reference to the relevant portion of the standard for this.
Alex B
I thought all members in a struct were public by default, private in a class.
Romain Hippeau
Yes, they are. In this case, the access specifiers are entirely vacuous as far as specifying access goes -- they're still allowed, and as pointed out above, they still mean something, just not anything related to specifying access.
Jerry Coffin