gpt4 book ai didi

c++ - C++ 标准中哪里记录了对象的内存布局?

转载 作者:太空狗 更新时间:2023-10-29 20:51:17 24 4
gpt4 key购买 nike

这是一个大问题,所以我要的是引用资料而不是小册子大小的答案。我正在浏览 Stroustrup 的 C++ 之旅,看起来对象的布局方式是内存是许多 C++ 功能设计的基础,例如POD 与聚合与具有虚拟成员的类。

不幸的是,Tour 本身并没有详细介绍这个主题,并且浏览一些标准引用资料(例如 C++ Primer 5ed 和 TCPPPL 4ed)的 ToC 并没有显示它们是否或在何处涵盖它。

最佳答案

[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.

[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 ]

还有[dcl.array]这表明数组在内存中是连续的,[class.bit]其中讨论了位域,以及 [intro.object]其中谈到了对象大小和重叠子对象的概念。

可能还有其他地方。没有一处。

关于c++ - C++ 标准中哪里记录了对象的内存布局?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50803202/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com