在C ++ Standard中何处记录了对象的内存布局? [英] Where in the C++ Standard is the memory layout of objects documented?

查看:108
本文介绍了在C ++ Standard中何处记录了对象的内存布局?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个大问题,所以我要求提供参考,而不是小册子大小的答案.我正在经历Stroustrup的C ++之旅,似乎对象的布局方式是内存是许多C ++功能设计的基础,例如POD与聚合与具有虚拟成员的类.

This is a big question, so I'm asking for a reference rather than a booklet-sized answer. I'm going through Stroustrup's Tour of C++, and it seems like the way objects are laid out is memory is fundamental to the design of many C++ features, e.g. PODs vs aggregates vs classes with virtual members.

不幸的是,Tour本身并没有详细介绍该主题,而浏览一些标准参考文献(例如C ++ Primer 5ed和TCPPPL 4ed)的ToC并不能显示它们是否涉及该主题.

Unfortunately, the Tour itself doesn't cover this subject in detail, and skimming the ToCs of some standard references such as C++ Primer 5ed and TCPPPL 4ed doesn't show whether or where they cover it.

推荐答案

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

如果标准布局类对象具有任何非静态数据成员,则其地址与其第一个非静态数据成员的地址相同.否则,其地址与其第一个基类子对象(如果有)的地址相同. [注意:因此,为实现适当的对齐,在标准布局结构对象中可能会存在未命名的填充,但在其开头不会存在. -注释"-[注释:该对象及其第一个子对象是指针可互换的([basic.compound],[expr.static.cast]). -注释]

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] 讨论位域,而<一个href ="http://eel.is/c++draft/intro.object" rel ="nofollow noreferrer"> [intro.object] ,它讨论了对象大小和重叠子对象的概念.

There is also [dcl.array] which indicates that arrays are contiguous in memory, [class.bit] which talks about bit-fields, and [intro.object] which talkes about object size and the concept of overlapping subobjects.

可能还有其他地方.没有一个地点.

There may be other places. There's no one spot.

这篇关于在C ++ Standard中何处记录了对象的内存布局?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆