是类的连续的内存布局吗? [英] Is Memory Layout for a class successive?

查看:154
本文介绍了是类的连续的内存布局吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我们声明一个类的对象是它的内存布局连续(一个接一个)?如果它的连续比 padding

When we declare object of a class is its memory layout successive(One after the other)?If its successive than does padding occurs in it (like structure padding)?Please help me out with the concepts of memory layout for a class

提前感谢。

它的内存分配连续(一个
在另一个之后)?
解决方案


标准没有提供任何此类保证。对象内存布局是实现定义的。

The Standard doesn't give any such guarantee. Object memory layout is implementation-defined.

通常,数据成员的内存地址按照它们在类中定义的顺序增加。但是这个顺序可能在访问指定符( private protected public )。这已在 C ++对象模型中通过 Lippman

Usually, memory address for data members increases in the order they're defined in the class . But this order may be disrupted at any place where the access-specifiers (private, protected, public) are encountered. This has been discussed in great detail in Inside the C++ Object Model by Lippman.

摘录自 C / C ++ Users Journal


编译器不允许这样做
重新排列本身。
标准要求在相同 public :, protected :,或
private:中的
的所有数据必须在该
订单中编译器。 如果
使用访问
说明符来穿插您的数据,编译器将允许
重新安排
访问限定符分隔的
数据块改进布局
这是
为什么有些人喜欢在每个数据
成员前面添加一个访问
说明符。

The compiler isn't allowed to do this rearrangement itself, though. The standard requires that all data that's in the same public:, protected:, or private: must be laid out in that order by the compiler. If you intersperse your data with access specifiers, though, the compiler is allowed to rearrange the access-specifier-delimited blocks of data to improve the layout, which is why some people like putting an access specifier in front of every data member.

有趣的是,不是吗?

这篇关于是类的连续的内存布局吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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