简体   繁体   English

GCC和Visual Studio(32位控制器)中结构大小的差异

[英]Difference of structure size in GCC and Visual Studio (32-bit controller)

As shown in below structure, 1 byte should be padded after var1 and since short is used in structure, one more byte is padded after var3 . 如下面结构中所示,1个字节后应被填充var1并且由于short的结构被使用,一个多字节之后填充var3 With this, the total should be 6. That is the value I am getting in Visual Studio. 这样,总数应该为6。这就是我在Visual Studio中获得的价值。 It is also mentioned at Wikipedia on Data structure alignment . Wikipedia上也提到了“ 数据结构对齐”

typedef struct {char var1; short var2; char var3;} Bytes;

But in GCC it is giving the size as 8 bytes. 但是在GCC中,它的大小为8个字节。 Please let me know about the behaviour. 请让我知道该行为。


Hi Jack, 嗨杰克,

I did experement with below structure. 我做了以下结构的实验。

typedef struct
{
    char  charVar1;
    short shortVar2;
    char  charVar3;
}tsByte;

printf("\n Sizeo of Byte        : %d", sizeof(Byte));
printf("\n Sizeo of charVar1    : %d", sizeof(Byte.charVar1));
printf("\n Sizeo of shortVar2   : %d", sizeof(Byte.shortVar2));
printf("\n Sizeo of charVar3    : %d", sizeof(Byte.charVar3));
printf("\n Address of charVar1  : %x", &Byte.charVar1);
printf("\n Address of shortVar2 : %x", &Byte.shortVar2);
printf("\n Address of shortVar3 : %x", &Byte.charVar3);

and the results are as below. 结果如下。

Sizeo of Byte        : 8
Sizeo of charVar1    : 1
Sizeo of shortVar2   : 2
Sizeo of charVar3    : 1
Address of charVar1  : 4007e90
Address of shortVar2 : 4007e92
Address of shortVar3 : 4007e94

Usually padding at the end of a structure is necessary to ensure proper alignment (of all of its members) when the structure is used as an element of an array. 当将结构用作数组的元素时,通常必须在结构的末尾填充以确保正确对齐(所有成员)。 But bit confused with padding for last element. 但是,最后一个元素的填充有点混乱。 Whether padding "for last element" is based on the 8/16/32-bit controller architecture or based on the biggest member size (here short). 是否为“最后一个元素”填充是基于8/16/32位控制器体系结构还是基于最大成员大小(此处简称)。

I feel in Visual studio it is based on biggest member size and so you are getting size as 6 bytes. 我觉得在Visual Studio中,它基于最大的成员大小,因此您得到的大小为6个字节。 Whereas with gcc compiler it is based on controller architecture and since I am using 32-bit controller it is aligned with 4-byte memory. 而对于gcc编译器,它基于控制器体系结构,并且由于我使用的是32位控制器,因此它与4字节内存对齐。 Because of this in gcc compiler it is 8 bytes. 因此,在gcc编译器中,它为8个字节。 Please correct me if I am wrong. 如果我错了,请纠正我。

You will need to write a bit more code to investigate the differences between compilers. 您将需要编写更多代码来研究编译器之间的差异。 For example, try a code fragment such as: 例如,尝试如下代码片段:

    // define a structure
    typedef struct {char var1; short var2; char var3;} Bytes;

    // allocate storage for the structure
    Bytes data;

    // tell me stuff about that structure
    printf("\nsizeof Bytes=%d, sizeof var1=%d, sizeof var2=%d, sizeof var3=%d",
         sizeof(data), sizeof(data.var1), sizeof(data.var2), sizeof(data.var3));

On Eclipse/Microsoft C compiler, I got: 在Eclipse / Microsoft C编译器上,我得到:

    sizeof Bytes=6, sizeof var1=1, sizeof var2=2, sizeof var3=1

So, why is Bytes=6 but the sizeof vars sum to 4? 那么,为什么字节数= 6但vars的大小总和为4? This is answered by the following: 可以通过以下方式回答:

    printf("\naddrof var1=%08x", &data.var1);
    printf("\naddrof var2=%08x", &data.var2);
    printf("\naddrof var3=%08x", &data.var3);

Which produces: 产生:

    addrof data=0012ff40
    addrof var1=0012ff40
    addrof var2=0012ff42
    addrof var3=0012ff44

And so, even though var1 is a char it uses two bytes, thus the Microsoft C compiler implements ANSI C as documented by K&R!! 因此,即使var1是一个char它也使用两个字节,因此Microsoft C编译器实现了K&R记录的ANSI C!

You will need to run similar code using GCC to determine exactly how it is formatting the struct Bytes . 您将需要使用GCC运行类似的代码来确定它如何格式化struct Bytes

As shown in below structure, 1 byte should be padded after 'var1' and since short is used in structure, one more byte is padded after 'var3'. 如下结构所示,在“ var1”之后应填充1个字节,并且由于在结构中使用short,因此在“ var3”之后应再填充一个字节。 With this, the total should be 6. 这样,总数应为6。

Wrong. 错误。 The C language does not stipulate what the total size must be -- it is implementation-defined. C语言没有规定总大小必须是多少,而是由实现定义的。 The compiler is free to add padding bytes between members or at the end of the structure as it sees fit. 编译器可以根据需要随意在成员之间或结构的末尾添加填充字节。 It may choose to add no padding, to align members on 2- or 4-byte boundaries, or something else entirely. 它可以选择不添加填充,以在2字节或4字节边界上对齐成员,或完全将其对齐。 All of those behaviors are allowed by the C standard. 所有这些行为都是C标准所允许的。

The fact that different compilers product different values just demonstrates that not all compilers are the same. 不同的编译器产生不同的值的事实仅说明并非所有编译器都是相同的。 Both compilers are compliant with the C standard in this regard. 在这方面,两个编译器均符合C标准。

If you require specific alignment, you can always user #pragma pack : 如果您需要特定的对齐方式,则始终可以使用#pragma pack

And as others have already noted, you can use the sizeof() operator and the Standard C offsetof() macro to determine the actual alignment for your particular combination of struct definition, compiler, and alignment settings. 正如其他人已经指出的那样,您可以使用sizeof()运算符和Standard C offsetof()宏来确定结构定义,编译器和对齐设置的特定组合的实际对齐方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM