C ++中的对象如何存储在内存中? [英] How are objects stored in memory in C++?

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

问题描述

如何将对象存储在C ++内存中?



对于常规类,如

  class Object 
{
public:
int i1;
int i2;
char i3;
int i4;
private:
};

使用Object的指针作为数组可以访问i1,如下所示:

 ((Object *)& myObject)[0] ===? 

有关SO的其他问题似乎暗示将一个结构体转换为指针将指向第一个成员POD类型。对于构造函数的类如果有什么不同呢?



编辑:



如果是非POD类型,

  [i1  -  4bytes] [i2  -  4bytes] [i3  - 1byte] [padding  -  3bytes] [i4  -  4bytes] 


解决方案

差不多。你投射到一个Object *,并且忽略了一个地址。让我们重新询问如下:

 ((int *)& myObject)[0] == i1 

你必须非常小心这样的假设。正如你定义的结构,这应该是真正的任何编译器,你可能会遇到。但是对象的所有其他属性(你可能从你的例子中省略)将像其他人说的那样,使它非POD,并且可能(可能以编译器相关的方式)使上面的语句不是真的。注意,我不会这么快告诉你它会工作,如果你问i3 - 在这种情况下,即使是纯POD,对齐或字节序可以很容易地



无论如何,如果可能,你应该避免这种事情。即使它现在工作正常,如果你(或任何人不明白你在做这个伎俩)改变结构顺序或添加新的字段,这个伎俩将失败,在你使用它的所有地方,这可能很难找到。



回答你的编辑:如果这是你的整个类定义,并且你使用一个主流编译器与默认选项,并运行在x86处理器,那么是的,你可能猜到正确的内存布局。但是编译器,编译器选项和不同的CPU架构的选择可能很容易使您的假设无效。


How are objects stored in memory in C++?

For a regular class such as

class Object
    {
public:
    int i1;
    int i2;
    char i3;
    int i4;
private:
    };

Using a pointer of Object as an array can be used to access i1 as follows?

((Object*)&myObject)[0] === i1?

Other questions on SO seem to suggest that casting a struct to a pointer will point to the first member for POD-types. How is this different for classes with constructors if at all? Also in what way is it different for non-POD types?

Edit:

In memory therefore would the above class be laid out like the following?

[i1 - 4bytes][i2 - 4bytes][i3 - 1byte][padding - 3bytes][i4 - 4bytes]

解决方案

Almost. You cast to an Object*, and neglected to take an address. Let's re-ask as the following:

((int*)&myObject)[0] == i1

You have to be really careful with assumptions like this. As you've defined the structure, this should be true in any compiler you're likely to come across. But all sorts of other properties of the object (which you may have omitted from your example) will, as others said, make it non-POD and could (possibly in a compiler-dependent way) make the above statement not true.

Note that I wouldn't be so quick to tell you it would work if you had asked about i3 -- in that case, even for plain POD, alignment or endianness could easily screw you up.

In any case, you should be avoiding this kind of thing, if possible. Even if it works fine now, if you (or anybody else who doesn't understand that you're doing this trick) ever changes the structure order or adds new fields, this trick will fail in all the places you've used it, which may be hard to find.

Answer to your edit: If that's your entire class definition, and you're using one of the mainstream compilers with default options, and running on an x86 processor, then yes, you've probably guessed the right memory layout. But choice of compiler, compiler options, and different CPU architecture could easily invalidate your assumptions.

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

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