Home > Backend Development > C++ > How does C arrange objects in memory, and what does it mean for dynamic casting and reinterpreting?

How does C arrange objects in memory, and what does it mean for dynamic casting and reinterpreting?

DDD
Release: 2024-11-02 03:52:30
Original
391 people have browsed it

How does C   arrange objects in memory, and what does it mean for dynamic casting and reinterpreting?

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)的空间要求也可能导致这种情况。
Copy after login

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
Copy after login

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);
}
Copy after login

After creating a source file that uses a class memory layout, clang will display the memory layout.

$ clang -cc1 -fdump-record-layouts layout.cpp
Copy after login

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]
Copy after login

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/
Copy after login

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=((&amp; Class::_ZTV5Class) + 24u)
  SBase1 (0x0x141f78840) 0
      primary-for Class (0x0x141f81280)
  SBase2 (0x0x141f788a0) 16
      vptr=((&amp; Class::_ZTV5Class) + 56u)
  SBase3 (0x0x141f78900) 28
  VBase (0x0x141f78960) 64 virtual
      vptridx=8u vbaseoffset=-24 vptr=((&amp; Class::_ZTV5Class) + 88u)
Copy after login

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:

  • gcc: $ g -fdump-class- hierarchy -c layout.cpp
  • Visual C: cl main.cpp /c /d1reportSingleClassLayoutTest_A

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template