内存布局C ++对象 [英] memory layout C++ objects

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

问题描述

我基本上想知道C ++如何在内存中定位对象。所以,我听说动态转换只是用一个偏移调整内存中的对象的指针;和重新解释类允许我们用这个指针做任何事情。我真的不明白这一点。

I am basically wondering how C++ lays out the object in memory. So, I hear that dynamic casts simply adjust the object's pointer in memory with an offset; and reinterpret kind of allows us to do anything with this pointer. I don't really understand this. Details would be appreciated!

推荐答案

每个类按声明顺序排列其数据成员。

编译器允许在成员之间放置填充以使访问有效(但不允许重新排序)。

Each class lays out its data members in the order of declaration.
The compiler is allowed to place padding between members to make access efficient (but it is not allowed to re-order).

如何 dynamic_cast< ; works是一个编译器实现细节,没有被标准定义。它将取决于编译器使用的ABI。

How dynamic_cast<> works is a compiler implementation detail and not defined by the standard. It will all depend on the ABI used by the compiler.

reinterpret_cast<> 的对象。你唯一可以保证的工作是,一个指向一个void *的指针,并返回到同一个指针类将给你相同的指针。

reinterpret_cast<> works by just changing the type of the object. The only thing that you can guarantee that works is that casting a pointer to a void* and back to the same the pointer to class will give you the same pointer.

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

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