Memory layout of C objects
Question:
How is C arranged in memory? Object? I heard that dynamic cast only adjusts the pointer of the object in memory, with offset; while reinterpretation allows us to do any operation on this pointer. I don't quite understand. Please provide details!
Answer:
Memory layout
Memory layout mainly depends on the implementation. There is one key exception, which is that member variables with the same access specifier will be arranged in declaration order.
§ 9.2.14 非静态数据成员(非联合)具有相同访问控制权的类(Clause 11)被分配,以便后续成员在类对象中具有更高的地址。具有不同访问控制权的非静态数据成员的分配顺序未指定(11)。实现对齐要求可能导致两个相邻成员不会立即相互分配;管理虚拟函数(10.3)和虚拟基类(10.1)的空间要求也可能导致这种情况。
Other memory management
In addition to member variables, a class or structure also needs to manage member variables, sub-objects of the base class, virtual functions (such as virtual table) and the space provided for the padding and alignment of this data. It depends on the implementation, but the Itanium ABI specification is a popular choice. gcc and clang follow it (at least to some extent).
http://mentorembedded.github.io/cxx-abi/abi.html#layout
Itanium ABI
Of course, the Itanium ABI is not part of the C standard and is not binding. For more detailed information, you'll need to consult the implementer's documentation and tools. clang provides a tool to view the memory layout of a class. For example, the following:
class VBase { virtual void corge(); int j; }; class SBase1 { virtual void grault(); int k; }; class SBase2 { virtual void grault(); int k; }; class SBase3 { void grault(); int k; }; class Class : public SBase1, SBase2, SBase3, virtual VBase { public: void bar(); virtual void baz(); // 不允许虚拟成员函数模板,原因考虑内存布局和虚表 // template<typename T> // virtual void quux(); private: int i; char c; public: float f; private: double d; public: short s; }; class Derived : public Class { virtual void qux(); }; int main() { return sizeof(Derived); }
After creating a source file that uses a class memory layout, clang will display the memory layout.
$ clang -cc1 -fdump-record-layouts layout.cpp
Layout of Class:
*** Dumping AST Record Layout 0 | class Class 0 | class SBase1 (primary base) 0 | (SBase1 vtable pointer) 8 | int k 16 | class SBase2 (base) 16 | (SBase2 vtable pointer) 24 | int k 28 | class SBase3 (base) 28 | int k 32 | int i 36 | char c 40 | float f 48 | double d 56 | short s 64 | class VBase (virtual base) 64 | (VBase vtable pointer) 72 | int j | [sizeof=80, dsize=76, align=8 | nvsize=58, nvalign=8]
More memory layout information
More about this clang Information about the feature can be found on Eli Bendersky's blog:
http://eli.thegreenplace.net/2012/12/17/dumping-a-c-objects-memory-layout-with-clang/
gcc provides a similar tool `-fdump-class-hierarchy'. For the class given above, it outputs (among other things):
Class Class size=80 align=8 base size=58 base align=8 Class (0x0x141f81280) 0 vptridx=0u vptr=((& Class::_ZTV5Class) + 24u) SBase1 (0x0x141f78840) 0 primary-for Class (0x0x141f81280) SBase2 (0x0x141f788a0) 16 vptr=((& Class::_ZTV5Class) + 56u) SBase3 (0x0x141f78900) 28 VBase (0x0x141f78960) 64 virtual vptridx=8u vbaseoffset=-24 vptr=((& Class::_ZTV5Class) + 88u)
It doesn't itemize the member variables (or at least I don't know how to get them), but you can see that they have to Located between offset 28 and 64, just like in clang layout.
You can see that a base class is designated as primary. This eliminates the need for this pointer adjustment when the Class is accessed as SBase1.
Other compiler directives
The following equivalent directives apply to different compilers:
See: https://blogs.msdn.microsoft.com/vcblog/2007/ 05/17/diagnosing-hidden-odr-violations-in-visual-c-and-fixing-lnk2022/
The above is the detailed content of How does C arrange objects in memory, and what does it mean for dynamic casting and reinterpreting?. For more information, please follow other related articles on the PHP Chinese website!