<tfoot id='VVtok'></tfoot>
<legend id='VVtok'><style id='VVtok'><dir id='VVtok'><q id='VVtok'></q></dir></style></legend>

    • <bdo id='VVtok'></bdo><ul id='VVtok'></ul>

      <small id='VVtok'></small><noframes id='VVtok'>

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

        C++ 数据成员对齐和数组打包

        C++ Data Member Alignment and Array Packing(C++ 数据成员对齐和数组打包)

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

            <small id='C9AA1'></small><noframes id='C9AA1'>

            • <bdo id='C9AA1'></bdo><ul id='C9AA1'></ul>

            • <legend id='C9AA1'><style id='C9AA1'><dir id='C9AA1'><q id='C9AA1'></q></dir></style></legend>

                • 本文介绍了C++ 数据成员对齐和数组打包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  在代码审查期间,我遇到了一些定义如下简单结构的代码:

                  During a code review I've come across some code that defines a simple structure as follows:

                  class foo {
                     unsigned char a;
                     unsigned char b;
                     unsigned char c;
                  }
                  

                  在其他地方,定义了这些对象的数组:

                  Elsewhere, an array of these objects is defined:

                  foo listOfFoos[SOME_NUM];
                  

                  稍后,这些结构被原始复制到缓冲区中:

                  Later, the structures are raw-copied into a buffer:

                  memcpy(pBuff,listOfFoos,3*SOME_NUM);
                  

                  此代码依赖于以下假设:a.) foo 的大小为 3,并且没有应用填充,并且 b.) 这些对象的数组被打包,它们之间没有填充.

                  This code relies on the assumptions that: a.) The size of foo is 3, and no padding is applied, and b.) An array of these objects is packed with no padding between them.

                  我已经在两个平台(RedHat 64b、Solaris 9)上用 GNU 尝试过它,它在两个平台上都有效.

                  I've tried it with GNU on two platforms (RedHat 64b, Solaris 9), and it worked on both.

                  上述假设是否有效?如果没有,在什么条件下(例如操作系统/编译器发生变化)可能会失败?

                  Are the assumptions above valid? If not, under what conditions (e.g. change in OS/compiler) might they fail?

                  推荐答案

                  对象数组必须是连续的,所以对象之间永远不会有填充,尽管填充可以添加到对象的末尾(产生几乎效果一样).

                  An array of objects is required to be contiguous, so there's never padding between the objects, though padding can be added to the end of an object (producing nearly the same effect).

                  鉴于您正在使用字符,假设通常是正确的,但 C++ 标准当然不能保证这一点.不同的编译器,甚至只是传递给当前编译器的标志的更改都可能导致在结构元素之间或结构的最后一个元素之后插入填充,或两者兼而有之.

                  Given that you're working with char's, the assumptions are probably right more often than not, but the C++ standard certainly doesn't guarantee it. A different compiler, or even just a change in the flags passed to your current compiler could result in padding being inserted between the elements of the struct or following the last element of the struct, or both.

                  这篇关于C++ 数据成员对齐和数组打包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Consistent pseudo-random numbers across platforms(跨平台一致的伪随机数)
                  Vary range of uniform_int_distribution(改变uniform_int_distribution的范围)
                  What is a seed in terms of generating a random number?(就生成随机数而言,种子是什么?)
                  Is 1.0 a valid output from std::generate_canonical?(1.0 是 std::generate_canonical 的有效输出吗?)
                  Getting big random numbers in C/C++(在 C/C++ 中获取大随机数)
                  What is the best way to generate random numbers in C++?(在 C++ 中生成随机数的最佳方法是什么?)

                  <small id='iQC55'></small><noframes id='iQC55'>

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

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

                          1. <tfoot id='iQC55'></tfoot>