C ++编译器可以重新排序结构中的元素 [英] Can a C++ compiler re-order elements in a struct

查看:249
本文介绍了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;
   ...
   ...
};

当我将输出写入文件时, someUnsignedLongArray (即 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.

以下是标准的相关部分:

Here's the relevant part of the standard:

第9.2.12节:


宣布没有$ b的
(非联合)类的非静态数据成员$ b intervening access-specifier是分配的
,以便以后的成员在类
对象中有
个更高的地址。由
访问限定符分隔的
非静态数据成员的分配顺序未指定
(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 ++编译器可以重新排序结构中的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆