c/c++ struct 内存对齐

来源:互联网 发布:linux每个文件夹占用 编辑:程序博客网 时间:2024/06/07 06:05

为了让CPU能够更舒服地访问到变量,struct中的各成员变量的存储地址有一套对齐的机制。这个机制概括起来有两点:第一,每个成员变量的首地址,必须是它的类型的对齐值的整数倍,如果不满足,它与前一个成员变量之间要填充(padding)一些无意义的字节来满足;第二,整个struct的大小,必须是该struct中所有成员的类型中对齐值最大者的整数倍,如果不满足,在最后一个成员后面填充。

各种类型的变量的align值如下,参考的是wikipedia的页面:Data structure alignment

The following typical alignments are valid for compilers from Microsoft, Borland, and GNU when compiling for 32-bit x86:

  • char (one byte) will be 1-byte aligned.
  • short (two bytes) will be 2-byte aligned.
  • An int (four bytes) will be 4-byte aligned.
  • float (four bytes) will be 4-byte aligned.
  • double (eight bytes) will be 8-byte aligned on Windows and 4-byte aligned on Linux.
  • long double (twelve bytes) will be 4-byte aligned on Linux.
  • Any pointer (four bytes) will be 4-byte aligned on Linux. (eg: char*, int*)

The only notable difference in alignment for a 64-bit linux system when compared to a 32 bit is:

  • double (eight bytes) will be 8-byte aligned.
  • long double (Sixteen bytes) will be 16-byte aligned.
  • Any pointer (eight bytes) will be 8-byte aligned.

这里写了个程序来验证这些事:

在64位linux下面运行这段代码的结果是:
#include <stdio.h>


struct s {
    char a;
    short b;
    char c;
    double d;
    char e;
};


int main() {


    struct s s1;


    printf("%d, %d, %d, %d, %d\n",
        (char*)(&s1.a) - (char*)(&s1),
        (char*)(&s1.b) - (char*)(&s1),
        (char*)(&s1.c) - (char*)(&s1),
        (char*)(&s1.d) - (char*)(&s1),
        (char*)(&s1.e) - (char*)(&s1));
    printf("%d\n", sizeof(struct s));


    return 0;
}
0, 2, 4, 8, 16
24

由于对齐机制的存在,实际上上面的struct在内存中是长这个样子的,共计24个字节:

struct s {
    char a;             //在地址为0的位置
    char padding1[1];   //由于下面一个元素是short,对齐字节数为2的位数,需要补1字节
    short b;            //对齐到了地址为2的位置
    char c;             //在地址为4的位置
    char padding2[3];   //由于下面一个元素是double,对齐字节数为8的倍数,需要补3字节
    double d;           //对齐到了地址为8的位置
    char e;             //在地址为16的位置
    char padding3[7];   //整个struct的大小需要是对齐数最大者,也就是double的8字节的整数倍
};

如果是在32位的linux下跑上面的程序,由于double的长度还是8字节,但是对齐是4字节的了,所以前面几个成员的位置不变,而最后的padding只需要补3个字节就可以了,所以输出的结果是0, 2, 4, 8, 16及20.

对于windows,其32位和64位下double都是8字节对齐的,所以在32位和64位下跑这个程序结果都是0, 2, 4, 8, 16及24.

最后,整个struct的大小的要求是对齐值最大者的整数倍,没有什么默认的4或者8的倍数一说。如果把上面程序中的a,b,c,d,e的类型全变成char,那么最后的他们的地址会是0,1,2,3,4,整个struct的大小 sizeof(struct s)的值是5,没有任何padding发生。

以上程序实验的环境在64位centos x64上的gcc 4.1.2(32位结果加-m32参数)及Visual Studio 2008上得出。

0 0
原创粉丝点击