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

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

      • <bdo id='ESZTp'></bdo><ul id='ESZTp'></ul>
    2. <legend id='ESZTp'><style id='ESZTp'><dir id='ESZTp'><q id='ESZTp'></q></dir></style></legend>
    3. C++ 编译器可以重新排序结构中的元素吗

      Can a C++ compiler re-order elements in a struct(C++ 编译器可以重新排序结构中的元素吗)
    4. <legend id='wrdpT'><style id='wrdpT'><dir id='wrdpT'><q id='wrdpT'></q></dir></style></legend>

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

          • <tfoot id='wrdpT'></tfoot>

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

                本文介绍了C++ 编译器可以重新排序结构中的元素吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                C++ 编译器(特别是 g++)能否对结构的内部元素重新排序?

                Can a C++ compiler (specifically g++) re-order the internal elements of a struct?

                我看到一些奇怪的行为,其中我的结构包含如下内容:

                I'm seeing some strange behaviour where I have a structure that contains something like the following:

                Struct SomeStruct{
                   ...
                   ...
                   long someLong;
                   long someLongArray[25];
                   unsigned long someUnsignedLong;
                   unsigned long someUnsignedLongArray[8];
                   unsigned long int someUnsignedLongInt;
                   ...
                   ...
                };
                

                当我将输出写入文件时,someUnsignedLongArraysomeLongArray 的顺序似乎颠倒了(即 someLongArray[] 中的元素出现在 someUnsignedLong 之后,someUnsignedLongArray[] 的元素出现在 someLong 之后).这可能吗??

                When I write output this to file, the order of someUnsignedLongArray and someLongArray seem to be reversed (i.e. the elements in someLongArray[] appear after someUnsignedLong and the elements of someUnsignedLongArray[] appear after someLong). Is this possible??

                谢谢

                更新:根据要求,我正在使用以下内容写出结构:

                Update: As requested, I am writing out the structure using the following:

                int fd = open(fspec,O_RDWR|O_CREAT|O_TRUNC,0666);
                int writeRes =  write(fd,(char *)&someStruct,sizeof(SomeStruct));
                

                为了完整性,这里是完整的结构:

                For completeness, here is the full struct:

                struct SomeStruct{
                byte someByte;
                byte someByteArray[6];
                char someChar;
                char someCharArray[5];
                char someCharArrayArray[3][5];
                short someShort;
                signed short someShortArray[2];
                unsigned short someUnsignedShort;
                unsigned short someUnsignedShortArray[8];
                int someInt;
                int someIntArray[3];
                int someIntArrayArrayArrayArray[4][3][2][6];
                int *pSomeInt;
                unsigned int someUnsignedInt;
                unsigned int someUnsignedIntArray[9];
                long someLong;
                long someLongArray[25];
                unsigned long someUnsignedLong;
                unsigned long someUnsignedLongArray[8];
                unsigned long int someUnsignedLongInt;
                long long someLongLong;
                long long someLongLongArray[5];
                bool someBool;
                bool someBoolArray[3];
                unsigned long long someUnsignedLongLong;
                unsigned long long someUnsignedLongLongArray[5];
                unsigned long long someUnsignedLongLongArrayArray[5][2];
                unsigned long long int *pSomeUnsignedLongLongInt;
                };
                

                推荐答案

                它通常不能重新排序元素,不行.

                It normally can't reorder elements, no.

                一个例外是如果有访问说明符将它们分开:

                An exception is if there's an access specifier separating them:

                struct Foo {    
                  A a;
                  B b;
                  C c;
                private:
                  D d;
                  E e;
                  F f;
                };
                

                abc 保证按此顺序存储,而def 保证按顺序存储.但是不能保证 abc 相对于 de 的存储位置f.

                a, b and c are guaranteed to be stored in this order, and d, e and f are guaranteed to be stored in order. But there is no guarantees about where a, b and c are stored relative to d, e and f.

                要记住的另一件事是编译器可以插入任意多的填充,即使它不重新排序任何内容.

                Another thing to keep in mind is that the compiler can insert as much padding as it likes, even if it doesn't reorder anything.

                这是标准的相关部分:

                第 9.2.12 节:

                Section 9.2.12:

                a 的非静态数据成员(非联合)类声明没有干预访问说明符是分配,以便后来的成员有一个类中的更高地址目的.分配顺序非静态数据成员由一个分隔未指定访问说明符(11.1)"

                Nonstatic data members of a (non-union) class declared without an intervening access-specifier are allocated so that later members have higher addresses within a class object. The order of allocation of nonstatic data members separated by an access-specifier is unspecified (11.1)"

                这篇关于C++ 编译器可以重新排序结构中的元素吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                相关文档推荐

                How to print vector#39;s data(如何打印矢量的数据)
                Visual C++ appends 0xCC (int3) bytes at the end of functions(Visual C++ 在函数末尾附加 0xCC (int3) 字节)
                How to use a variable inside a _T wrapper?(如何在 _T 包装器中使用变量?)
                MSVC++ warning flags(MSVC++ 警告标志)
                How to read file which contains uxxxx in vc++(如何在vc++中读取包含uxxxx的文件)
                stack overflow error in C++ program(C++程序中的堆栈溢出错误)
                  • <bdo id='qqIJB'></bdo><ul id='qqIJB'></ul>
                    <i id='qqIJB'><tr id='qqIJB'><dt id='qqIJB'><q id='qqIJB'><span id='qqIJB'><b id='qqIJB'><form id='qqIJB'><ins id='qqIJB'></ins><ul id='qqIJB'></ul><sub id='qqIJB'></sub></form><legend id='qqIJB'></legend><bdo id='qqIJB'><pre id='qqIJB'><center id='qqIJB'></center></pre></bdo></b><th id='qqIJB'></th></span></q></dt></tr></i><div id='qqIJB'><tfoot id='qqIJB'></tfoot><dl id='qqIJB'><fieldset id='qqIJB'></fieldset></dl></div>

                          <tbody id='qqIJB'></tbody>
                        <tfoot id='qqIJB'></tfoot>

                        1. <legend id='qqIJB'><style id='qqIJB'><dir id='qqIJB'><q id='qqIJB'></q></dir></style></legend>

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