<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++ 编译器可以重新排序结构中的元素吗

      时间:2023-07-01
    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++ 编译器可以重新排序结构中的元素吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:如何在 C++ 中使用自定义类型作为映射的键? 下一篇:如何创建 std::set 结构?

                相关文章

                • <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>

                1. <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'>