9

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.

ngoozeff
  • 4,576
  • 3
  • 28
  • 21
  • You've got bigger problems. Heap alignment isn't usually better than 8 for example. __declspec(align) and _aligned_malloc() on MSVC. – Hans Passant Aug 11 '10 at 03:20
  • You might want to try writing simple code that accesses all members of a structure and look at the assembler output of your compiler (-S for gcc, if I remember correctly) to see how access happens. – JUST MY correct OPINION Aug 11 '10 at 03:26
  • @Hans: We're doing the alignment by hand for that reason. – ngoozeff Aug 11 '10 at 04:07

6 Answers6

15

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
  • 476,176
  • 80
  • 629
  • 1,111
  • The reordering bit seems troubling. I'd love to have a reference to the relevant portion of the standard for this. – Alex B Aug 11 '10 at 03:22
  • I thought all members in a struct were public by default, private in a class. – Romain Hippeau Aug 11 '10 at 04:23
  • 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 Aug 11 '10 at 04:27
5

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 --- ex-moderator kitten
  • 98,632
  • 24
  • 142
  • 234
  • 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 Aug 11 '10 at 03:24
  • @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 --- ex-moderator kitten Aug 11 '10 at 03:37
4

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
  • 24,113
  • 5
  • 60
  • 79
2

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.

Community
  • 1
  • 1
dan04
  • 87,747
  • 23
  • 163
  • 198
  • @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 Aug 11 '10 at 03:14
  • @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 Aug 11 '10 at 03:22
1

If you want to see the memory layout of all class types in memory under MSVC, add the /d1reportAllClassLayout switch to the compiler command line.

If you want to see what one class is layed out like, add /d1reportSingleClassLayoutNNNNN where NNNNN is the name of the class.

Xeo
  • 129,499
  • 52
  • 291
  • 397
SiliconValley
  • 1,465
  • 17
  • 30
0

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
  • 920
  • 1
  • 11
  • 20
  • That's not determined by the operating system, but mostly by the CPU architecture and the compiler. – wallyk Aug 11 '10 at 03:21
  • What makes you think that, out of curiosity? The textbook for my college Computing Systems class says otherwise (Computer Systems: A Programmer's Perspective, by Bryant & O'Hallaron). As far as I'm aware, even when you compile using g++ in cygwin it behaves differently than g++ in Linux. – Alex Zylman Aug 11 '10 at 04:20
  • 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 Aug 11 '10 at 09:16