代码:
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <stdlib.h>
/*
** 结构体中按照最长的一个成员变量大小对齐
**/
/**************************************
* | 1 | 2 |
* -------------
* | us1 |
* | us2 |
* | us3 |
* -------------
* 6
*/
struct A_S {
unsigned short us1;
unsigned short us2;
unsigned short us3;
};
/***************************************
* | 1 | 2 | 3 | 4 |
* -------------------------
* | uc | |
* | ui |
* | us | |
* -------------------------
* 12
*/
struct B_S {
unsigned char uc;
unsigned int ui;
unsigned short us;
};
/****************************************
* | 1 |
* -------
* | uc1 |
* | uc2 |
* | uc3 |
* -------
* 3
*/
struct C_S {
unsigned char uc1;
unsigned char uc2;
unsigned char uc3;
};
/****************************************
* | 1 | 2 | 3 | 4 |
* -------------------------
* | uc1 | |
* | ui |
* | uc2 | |
* -------------------------
* 12
*/
struct D_S {
unsigned char uc1;
unsigned int ui;
unsigned char uc2;
};
/****************************************
* | 1 | 2 |
* -------------
* | uc1 | |
* | us |
* | uc2 | |
* -------------
* 6
*/
struct E_S {
unsigned char uc1;
unsigned short us;
unsigned char uc2;
};
/****************************************
* | 1 | 2 | 3 | 4 |
* -------------------------
* | uc | | us1 |
* | us2 | |
* | ui |
* -------------------------
* 12
*/
struct F_S {
unsigned char uc;
unsigned short us[2];
unsigned int ui;
};
/****************************************
* | 1 | 2 | 3 | 4 |
* -------------------------
* | uc | | us |
* | ui |
* -------------------------
* 8
*/
struct G_S {
unsigned char uc;
unsigned short us;
unsigned int ui;
};
/****************** 32bit **********************
* | 1 | 2 | 3 | 4 |
* -------------------------
* | uc1 | |
* | d |
* | d |
* | uc2 | |
* -------------------------
* 16byte
*/
/****************** 64bit ****************************
* | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
* -------------------------------------------------
* | uc1 | |
* | d |
* | uc2 | |
* -------------------------------------------------
* 24byte
*/
struct H_S {
unsigned char uc1;
double d;
unsigned char uc2;
};
struct TEST{ //32bit:16+8=24, 64bit:24+16=40
unsigned char uc1;
struct H_S hs;
unsigned char uc2;
};
/****************** 32bit **********************
* | 1 | 2 | 3 | 4 |
* -------------------------
* | uc1 | |
* | d |
* | uc2 | |
* -------------------------
* 12byte
*/
/****************** 64bit ****************************
* | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
* -------------------------------------------------
* | uc1 | |
* | d |
* | uc2 | |
* -------------------------------------------------
* 24byte
*/
struct I_S {
unsigned char uc1;
long d;
unsigned char uc2;
};
int main(){
struct A_S as;
printf("sizeof(struct A_S)=%d\t",sizeof(struct A_S));
printf("us1=%p,us2=%p,us3=%p\n",&as.us1,&as.us2,&as.us3);
struct B_S bs;
printf("sizeof(struct B_S)=%d\t",sizeof(struct B_S));
printf("uc=%p,ui=%p,us=%p\n",&bs.uc,&bs.ui,&bs.us);
struct C_S cs;
printf("sizeof(struct C_S)=%d\t",sizeof(struct C_S));
printf("uc1=%p,uc2=%p,uc3=%p\n",&cs.uc1,&cs.uc2,&cs.uc3);
struct D_S ds;
printf("sizeof(struct D_S)=%d\t",sizeof(struct D_S));
printf("uc1=%p,ui=%p,uc2=%p\n",&ds.uc1,&ds.ui,&ds.uc2);
struct E_S es;
printf("sizeof(struct E_S)=%d\t",sizeof(struct E_S));
printf("uc1=%p,us=%p,uc2=%p\n",&es.uc1,&es.us,&es.uc2);
struct F_S fs;
printf("sizeof(struct F_S)=%d\t",sizeof(struct F_S));
printf("uc=%p,us1=%p,us2=%p,ui=%p\n",&fs.uc,&fs.us[0],&fs.us[1],&fs.ui);
struct G_S gs;
printf("sizeof(struct G_S)=%d\t",sizeof(struct G_S));
printf("uc=%p,us=%p,ui=%p\n",&gs.uc,&gs.us,&gs.ui);
struct H_S hs;
printf("sizeof(struct H_S)=%d\t",sizeof(struct H_S));
printf("uc1=%p,d=%p,uc2=%p\n",&hs.uc1,&hs.d,&hs.uc2);
printf("sizeof(struct TEST)=%d\n",sizeof(struct TEST));
struct I_S is;
printf("sizeof(struct I_S)=%d\t",sizeof(struct I_S));
printf("uc1=%p,d=%p,uc2=%p\n",&is.uc1,&is.d,&is.uc2);
return 0;
}
结果:
32bit
$ ./test
sizeof(struct A_S)=6 us1=0xbf88569c,us2=0xbf88569e,us3=0xbf8856a0
sizeof(struct B_S)=12 uc=0xbf8856b0,ui=0xbf8856b4,us=0xbf8856b8
sizeof(struct C_S)=3 uc1=0xbf885699,uc2=0xbf88569a,uc3=0xbf88569b
sizeof(struct D_S)=12 uc1=0xbf8856bc,ui=0xbf8856c0,uc2=0xbf8856c4
sizeof(struct E_S)=6 uc1=0xbf8856a2,us=0xbf8856a4,uc2=0xbf8856a6
sizeof(struct F_S)=12 uc=0xbf8856c8,us1=0xbf8856ca,us2=0xbf8856cc,ui=0xbf8856d0
sizeof(struct G_S)=8 uc=0xbf8856a8,us=0xbf8856aa,ui=0xbf8856ac
sizeof(struct H_S)=16 uc1=0xbf8856e0,d=0xbf8856e4,uc2=0xbf8856ec
sizeof(struct TEST)=24
sizeof(struct I_S)=12 uc1=0xbf8856d4,d=0xbf8856d8,uc2=0xbf8856dc
64bit
$ ./test
sizeof(struct A_S)=6 us1=0x7ffc58a92400,us2=0x7ffc58a92402,us3=0x7ffc58a92404
sizeof(struct B_S)=12 uc=0x7ffc58a92430,ui=0x7ffc58a92434,us=0x7ffc58a92438
sizeof(struct C_S)=3 uc1=0x7ffc58a923f0,uc2=0x7ffc58a923f1,uc3=0x7ffc58a923f2
sizeof(struct D_S)=12 uc1=0x7ffc58a92440,ui=0x7ffc58a92444,uc2=0x7ffc58a92448
sizeof(struct E_S)=6 uc1=0x7ffc58a92410,us=0x7ffc58a92412,uc2=0x7ffc58a92414
sizeof(struct F_S)=12 uc=0x7ffc58a92450,us1=0x7ffc58a92452,us2=0x7ffc58a92454,ui=0x7ffc58a92458
sizeof(struct G_S)=8 uc=0x7ffc58a92420,us=0x7ffc58a92422,ui=0x7ffc58a92424
sizeof(struct H_S)=24 uc1=0x7ffc58a92460,d=0x7ffc58a92468,uc2=0x7ffc58a92470
sizeof(struct TEST)=40
sizeof(struct I_S)=24 uc1=0x7ffc58a92480,d=0x7ffc58a92488,uc2=0x7ffc58a92490