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;
...
...
};
当我将输出写入文件时,someUnsignedLongArray 和 someLongArray 的顺序似乎颠倒了(即 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;
};
a
、b
和 c
保证按此顺序存储,而d
、e
和 f
保证按顺序存储.但是不能保证 a
、b
和 c
相对于 d
、e 的存储位置
和 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++ 编译器可以重新排序结构中的元素吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!