<tfoot id='DN0uy'></tfoot>

        <i id='DN0uy'><tr id='DN0uy'><dt id='DN0uy'><q id='DN0uy'><span id='DN0uy'><b id='DN0uy'><form id='DN0uy'><ins id='DN0uy'></ins><ul id='DN0uy'></ul><sub id='DN0uy'></sub></form><legend id='DN0uy'></legend><bdo id='DN0uy'><pre id='DN0uy'><center id='DN0uy'></center></pre></bdo></b><th id='DN0uy'></th></span></q></dt></tr></i><div id='DN0uy'><tfoot id='DN0uy'></tfoot><dl id='DN0uy'><fieldset id='DN0uy'></fieldset></dl></div>

          <bdo id='DN0uy'></bdo><ul id='DN0uy'></ul>
      1. <small id='DN0uy'></small><noframes id='DN0uy'>

        <legend id='DN0uy'><style id='DN0uy'><dir id='DN0uy'><q id='DN0uy'></q></dir></style></legend>

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

        时间:2023-09-15

          <tbody id='uWdeg'></tbody>

        <i id='uWdeg'><tr id='uWdeg'><dt id='uWdeg'><q id='uWdeg'><span id='uWdeg'><b id='uWdeg'><form id='uWdeg'><ins id='uWdeg'></ins><ul id='uWdeg'></ul><sub id='uWdeg'></sub></form><legend id='uWdeg'></legend><bdo id='uWdeg'><pre id='uWdeg'><center id='uWdeg'></center></pre></bdo></b><th id='uWdeg'></th></span></q></dt></tr></i><div id='uWdeg'><tfoot id='uWdeg'></tfoot><dl id='uWdeg'><fieldset id='uWdeg'></fieldset></dl></div>
          <tfoot id='uWdeg'></tfoot>
            <bdo id='uWdeg'></bdo><ul id='uWdeg'></ul>

              • <small id='uWdeg'></small><noframes id='uWdeg'>

                <legend id='uWdeg'><style id='uWdeg'><dir id='uWdeg'><q id='uWdeg'></q></dir></style></legend>
                1. 本文介绍了C++中对象是如何存储在内存中的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

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

                  对于常规类,例如

                  class 对象{上市:国际 i1;国际 i2;字符 i3;国际 i4;私人的:};

                  使用Object的指针作为数组可以如下访问i1吗?

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

                  关于 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++中对象是如何存储在内存中的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:未定义的行为可以擦除硬盘驱动器吗? 下一篇:如何在 C++ 中获取进程的起始/基地址?

                  相关文章

                  最新文章

                  <tfoot id='Ck5fO'></tfoot>
                  <i id='Ck5fO'><tr id='Ck5fO'><dt id='Ck5fO'><q id='Ck5fO'><span id='Ck5fO'><b id='Ck5fO'><form id='Ck5fO'><ins id='Ck5fO'></ins><ul id='Ck5fO'></ul><sub id='Ck5fO'></sub></form><legend id='Ck5fO'></legend><bdo id='Ck5fO'><pre id='Ck5fO'><center id='Ck5fO'></center></pre></bdo></b><th id='Ck5fO'></th></span></q></dt></tr></i><div id='Ck5fO'><tfoot id='Ck5fO'></tfoot><dl id='Ck5fO'><fieldset id='Ck5fO'></fieldset></dl></div>

                    <legend id='Ck5fO'><style id='Ck5fO'><dir id='Ck5fO'><q id='Ck5fO'></q></dir></style></legend>
                        <bdo id='Ck5fO'></bdo><ul id='Ck5fO'></ul>

                    1. <small id='Ck5fO'></small><noframes id='Ck5fO'>