测试内存字节对齐代码

//by www.datahf.net zhangyu
#include "stdio.h" 
typedef struct A 
{ 
    char c; 
    int d; 
    short e; 
 
}; 
typedef struct B 
{ 
    char c; 
    __int64 d; 
    int e; 
    short f; 
    A g; 
    char h; 
    int i; 
}; 
typedef struct C 
{ 
    char c; 
    __int64 d; 
    int e; 
    short f; 
    char g; 
    short h; 
}; 
typedef struct D 
{ 
    char a; 
    short b; 
    char c; 
}; 
int main() 
{ 
 
    B *b=new B; 
    void *s[32]; 
    s[0]=b; 
    s[1]=&b->c; 
    s[2]=&b->d; 
    s[3]=&b->e; 
    s[4]=&b->f; 
    s[5]=&b->g; 
    s[6]=&b->h; 
    s[7]=&b->g.c; 
    s[8]=&b->g.d; 
    s[9]=&b->g.e; 
    s[10]=&b->i; 
    b->c= 0x11; 
    b->d= 0x2222222222222222; 
    b->e= 0x33333333; 
    b->f=0x4444; 
    b->g.c=0x50; 
    b->g.d=0x51515151; 
    b->g.e=0x5252; 
    b->h=0x66; 
    int i1=sizeof(A); 
    int i2=sizeof(B); 
    int i3=sizeof(C); 
    int i4=sizeof(D); 
    printf("i1:%d\ni2:%d\ni3:%d\ni4:%d\n",i1,i2,i3,i4);//12 48 32 6 
} 


你可能感兴趣的:(C++,内存对齐方式)