MSDN中关于结构成员对齐的一个例子

来源:互联网 发布:淘宝店铺背景设置 编辑:程序博客网 时间:2024/05/30 23:37

//以下默认的结构成员对齐为8字节

 

The following sample shows how to the pack pragma to change the alignment of a structure.

 
// pragma_directives_pack.cpp
#include <stddef.h>
#include <stdio.h>

struct S {
   int i;   // size 4
   short j;   // size 2
   double k;   // size 8
};

#pragma pack(2)
struct T {
   int i;
   short j;
   double k;
};

int main() {
   printf("%d ", offsetof(S, i));
   printf("%d ", offsetof(S, j));
   printf("%d/n", offsetof(S, k));

   T tt;
   printf("%d ", offsetof(T, i));
   printf("%d ", offsetof(T, j));
   printf("%d/n", offsetof(T, k));
}

-----------------
Result:
0 4 80 4 6