4

This is a big question, so I'm asking for a reference rather than a booklet-sized answer. I'm going through Stroustrup's Tour of C++, and it seems like the way objects are laid out is memory is fundamental to the design of many C++ features, e.g. PODs vs aggregates vs classes with virtual members.

Unfortunately, the Tour itself doesn't cover this subject in detail, and skimming the ToCs of some standard references such as C++ Primer 5ed and TCPPPL 4ed doesn't show whether or where they cover it.

curiousguy
  • 8,038
  • 2
  • 40
  • 58
Dun Peal
  • 16,679
  • 11
  • 33
  • 46

1 Answers1

7

[class.mem]/18:

Non-static data members of a (non-union) class with the same access control are allocated so that later members have higher addresses within a class object. The order of allocation of non-static data members with different access control is unspecified. Implementation alignment requirements might cause two adjacent members not to be allocated immediately after each other; so might requirements for space for managing virtual functions and virtual base classes.

and [class.mem]/25:

If a standard-layout class object has any non-static data members, its address is the same as the address of its first non-static data member. Otherwise, its address is the same as the address of its first base class subobject (if any). [ Note: There might therefore be unnamed padding within a standard-layout struct object, but not at its beginning, as necessary to achieve appropriate alignment. — end note ] [ Note: The object and its first subobject are pointer-interconvertible ([basic.compound], [expr.static.cast]). — end note ]

There is also [dcl.array] which indicates that arrays are contiguous in memory, [class.bit] which talks about bit-fields, and [intro.object] which talkes about object size and the concept of overlapping subobjects.

There may be other places. There's no one spot.

Barry
  • 286,269
  • 29
  • 621
  • 977
  • That's a good reference, but I am finding statements in other places that claim additional requirements, for instance [in cppreference](http://en.cppreference.com/w/cpp/language/data_members#Standard_layout) adding details like: "a pointer to an object of standard-layout struct type can be reinterpret_cast to pointer to its first non-static data member (if it has non-static data members) or otherwise its first base class subobject (if it has any), and vice versa. (padding is not allowed before the first data member). Note that strict aliasing rules still apply to the result of such cast". – Dun Peal Jun 11 '18 at 21:28
  • There's also bit fields and arrays. – Passer By Jun 12 '18 at 09:26
  • @PasserBy Serves me right for claiming completeness. – Barry Jun 12 '18 at 13:12
  • @DunPeal "_can be reinterpret_cast_" 1) That's a guarantee on an expression, not on layout. The guarantee on layout can exist in theory without the guarantee on expression. 2) Actually, C++ is deeply divided on when a pointer is pointing to an object. One side of C++ says that pointers must be correctly derived, the other side says that any pointer containing the correct address points to any object that happens to live at that address. So **there are two C++, one with high level semantic, the other with low level semantic.** You need to say which one you are talking about. – curiousguy Jun 13 '18 at 22:33