3

在 C++ 中,我认为 C++ 标准与数据成员在类中的排列方式无关,就内存布局而言?我认为这取决于所讨论的编译器是否正确?

我对学习如何在物理内存中表示对象和其他 C++ 实体(结构等)非常感兴趣(我知道列表是节点到节点,数组是连续内存 - 但语言的所有其他方面)。

编辑:学习 x86 汇编器会对此有所帮助并更好地理解 C++ 吗?

4

2 回答 2

6

The C++ standard does specify a few things, but far from everything.

The main rules are these:

  • objects in an array are laid out contiguously, with no padding between them.
  • class member objects not separated by an access specifier (public:/private:/protected:) are laid out in memory in the order in which they're declared, but there may be an unspecified amount of padding between member objects.
  • for certain types (standard-layout structs, in standardese terminology), the first base class, or if none exists, the first member, is laid out at the same address that the class itself.

There are a few more bits and pieces specified by the standard, but on the whole, the remaining details are really down to the compiler.

于 2012-10-14T18:03:46.437 回答
2

是的,标准没有说明对象在内存中的表示方式。要了解普通 C++ 对象是如何表示的,请阅读本书:C++ 对象模型内部。

于 2012-10-14T18:03:00.637 回答