结构对齐填充,最大填充大小和结构成员顺序 [英] Structure alignment padding, largest size of padding, and order of struct members

查看:80
本文介绍了结构对齐填充,最大填充大小和结构成员顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

自从我发现sizeof()运算符没有返回我期望的值以来,我一直在学习结构数据填充.根据我观察到的模式,它将结构成员与最大的数据类型对齐.例如...

I've been learning about structure data padding since I found out my sizeof() operator wasn't returning what I expected. According to the pattern that I've observed, it aligns structure members with the largest data type. So for example...

struct MyStruct1
{
    char a;     // 1 byte
    char b;     // 1 byte
    char c;     // 1 byte
    char d;     // 1 byte
    char e;     // 1 byte
                // Total 5 Bytes

    //Total size of struct = 5 (no padding)
};

struct MyStruct2
{
    char a;     // 1 byte
    char b;     // 1 byte
    char c;     // 1 byte
    char d;     // 1 byte
    char e;     // 1 byte
    short f;    // 2 bytes
                // Total 7 Bytes

    //Total size of struct = 8 (1 byte of padding between char e and short f
};

struct MyStruct3
{
    char a;         // 1 byte
    char b;         // 1 byte
    char c;         // 1 byte
    char d;         // 1 byte
    char e;         // 1 byte
    int f;          // 4 bytes
                    // Total 9 bytes

    //Total size of struct = 12 (3 bytes of padding between char e and int f
};

但是,如果将最后一个成员设为8字节数据类型(例如long long),则它仍仅会添加3字节的填充,从而形成四字节对齐的结构.但是,如果我以64位模式构建,则实际上它会对齐8个字节(最大的数据类型).我的第一个问题是,我说这使成员与最大数据类型保持一致,这是错误的吗?该语句对于64位版本似乎是正确的,但在32位版本中最多只能显示4个字节的数据类型.这与CPU的本机字"大小有关吗?还是程序本身?

However if make the last member an 8 byte data type, for example a long long, it still only adds 3 bytes of padding, making a four-byte aligned structure. However if I build in 64 bit mode, it does in fact align for 8 bytes (the biggest data type). My first question is, am I wrong in saying it aligns the members with the largest data type? This statement seems correct for a 64 bit build, but only true up to 4 byte data types in a 32 bit build. Has this to do with the native 'word' size of the CPU? Or the program itself?

我的第二个问题是,以下内容是否会完全浪费空间和错误的编程?

My second question is, would the following be an entire waste of space and bad programming?

struct MyBadStruct
{
    char a;             // 1 byte
    unsigned int b;     // 4 bytes
    UINT8 c;            // 1 byte
    long d;             // 4 bytes
    UCHAR e;            // 1 byte
    char* f;            // 4 bytes 
    char g;             // 1 byte
                        // Total of 16 bytes

    //Total size of struct = 28 bytes (12 bytes of padding, wasted)
};

谢谢.

推荐答案

填充的方式不是标准的一部分.因此,可以在不同的系统和编译器上以不同的方式进行操作.通常这样做是为了使变量按该大小对齐,即size = 1->无对齐,size = 2-> 2字节对齐,size = 4-> 4字节对齐,依此类推.对于size = 8,通常对齐4或8个字节.自身的结构通常对齐4或8个字节.但是-只是重复一遍-它取决于系统/编译器.

How padding is done, is not part of the standard. So it can be done differently on different systems and compilers. It is often done so that variables are aligned at there size, i.e. size=1 -> no alignment, size=2 -> 2 byte alignment, size=4 -> 4 byte alignment and so on. For size=8, it is normally 4 or 8 bytes aligned. The struct it self is normally 4 or 8 bytes aligned. But - just to repeat - it is system/compiler dependent.

就您而言,它似乎遵循上述模式.

In your case it seems to follow the pattern above.

所以

char a;
int  b;

将3字节填充为4字节,将int对齐.

will give 3 bytes padding to 4 byte align the int.

char a1;
int  b1;
char a2;
int  b2;
char a3;
int  b3;
char a4;
int  b4;

将以32字节结尾(再次将int对齐为4字节).

will end up as 32 byte (again to 4 byte align the int).

但是

int  b1;
int  b2;
int  b3;
int  b4;
char a1;
char a2;
char a3;
char a4;

由于int已经对齐,因此只有20.

will be just 20 as the int is already aligned.

因此,如果内存很重要,则将最大的成员放在首位.

So if memory matters, put the largest members first.

但是,如果内存无关紧要(例如,因为未使用该结构太多),则最好将内容保持逻辑顺序,以便人类易于阅读.

However, if memory doesn't matter (e.g. because the struct isn't used that much), it may be better to keep things in a logical order so that the code is easy to read for humans.

这篇关于结构对齐填充,最大填充大小和结构成员顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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