TITLE: object layout details (Newsgroups: comp.std.c++, 11 Aug 98) CHANG: Gilbert Chang writes: >Does the standard have anything to say about how C++ objects are layed out in >memory, especially when virtual functions are involved? CLAMAGE: clamage@Eng (Steve Clamage) Hardly anything. The implementation has to tell you the size and alignment requirements of the basic types (int, char, double, etc). Some of this information is also deducible from the various headers, especially , , . POD-structs must be laid out the same as in C. (A "POD-struct" is roughly a struct or union that has no C++ features that could cause it to differ from a C struct or union.) Between access modifiers ("private", "protected", "public") class members must be in non-decreasing address order, except for bitfields. Apart from those things, layout is entirely up to the implementation, and layout details of classes need not even be documented. Implementations typically do document layout, and unless constrained by a standard for the platform, different compilers (or different releases of the same compiler) often use different layouts. ABERG: haberg@REMOVE.matematik.su.se (Hans Aberg) > Does the C++ standard guarantee, in the case of single inheritance, that >the cast of a pointer to a base class make that pointer to point to the >full object? CLAMAGE: stephen.clamage@sun.com (Steve Clamage) There are no guarantees about relative positions of base classes in an object. The standard explictly says the layout is unspecified, which means the implementation doesn't even have to tell you what that layout is. You cannot assume even for single inheritance that no pointer adjustment will occur when casting between Derived* and Base*. It is common but not required that for single inheritance the address of any base class is the same as the complete object. Some implementations put the base part after the derived part for various reasons, in which case every cast involves a pointer adjustment.