可以 C++ 编译器对结构中的元素重新排序
C++ 编译器(特别是 g++)可以对结构体的内部元素重新排序吗?
我看到一些奇怪的行为,其中我有一个包含类似以下内容的结构:
Struct SomeStruct{
...
...
long someLong;
long someLongArray[25];
unsigned long someUnsignedLong;
unsigned long someUnsignedLongArray[8];
unsigned long int someUnsignedLongInt;
...
...
};
当我将输出写入文件时, someUnsignedLongArray 和 someLongArray 的顺序似乎是反转(即 someLongArray[] 中的元素出现在 someUnsignedLong 之后,someUnsignedLongArray[] 中的元素出现在 someLong 之后)。 这可能吗??
感谢
更新: 根据要求,我使用以下内容编写结构:
int fd = open(fspec,O_RDWR|O_CREAT|O_TRUNC,0666);
int writeRes = write(fd,(char *)&someStruct,sizeof(SomeStruct));
为了完整性,这里是完整的结构:
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;
};
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;
...
...
};
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??
Thanks
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;
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它通常不能重新排序元素,不是。
例外情况是,如果有访问说明符分隔它们:
a
、b
和c
保证按此顺序存储,并且d
、e
和f
保证按顺序存储。 但无法保证a
、b
和c
相对于d
、e 的存储位置
和f
。另一件需要记住的事情是,编译器可以插入任意多的填充,即使它不重新排序任何内容。
以下是该标准的相关部分:
第 9.2.12 节:
It normally can't reorder elements, no.
An exception is if there's an access specifier separating them:
a
,b
andc
are guaranteed to be stored in this order, andd
,e
andf
are guaranteed to be stored in order. But there is no guarantees about wherea
,b
andc
are stored relative tod
,e
andf
.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.
Here's the relevant part of the standard:
Section 9.2.12:
不能,请参阅自动字段重新排序避免填充的 C 结构 和为什么 GCC 不优化结构?了解更多信息。
我不知道“反转”是什么意思,也许您应该添加一些代码和输出。
It can't, see Automated field re-ordering in C structs to avoid padding and Why doesn't GCC optimize structs? for further information.
I don't know what you mean with "reversed", perhaps you should add some code and the output.