结构体内存对齐测试

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/chenyefei/article/details/86508829

代码:

#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;
};

/****************************************
* |  1  |  2  |  3  |  4  |
* -------------------------
* |  uc1  |               |
* |           d           |
* |           d           |
* |  uc2  |               |
* -------------------------
*            16
*/
struct H_S {
    unsigned char  uc1;
    double   d;
    unsigned char  uc2;
};

struct TEST{
    unsigned char  uc1;
    struct H_S  hs;
    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));
        return 0;
}

结果:

$ ./test 
sizeof(struct A_S)=6    us1=0xbfb56938,us2=0xbfb5693a,us3=0xbfb5693c
sizeof(struct B_S)=12   uc=0xbfb5694c,ui=0xbfb56950,us=0xbfb56954
sizeof(struct C_S)=3    uc1=0xbfb56935,uc2=0xbfb56936,uc3=0xbfb56937
sizeof(struct D_S)=12   uc1=0xbfb56958,ui=0xbfb5695c,uc2=0xbfb56960
sizeof(struct E_S)=6    uc1=0xbfb5693e,us=0xbfb56940,uc2=0xbfb56942
sizeof(struct F_S)=12   uc=0xbfb56964,us1=0xbfb56966,us2=0xbfb56968,ui=0xbfb5696c
sizeof(struct G_S)=8    uc=0xbfb56944,us=0xbfb56946,ui=0xbfb56948
sizeof(struct H_S)=16   uc1=0xbfb56970,d=0xbfb56974,uc2=0xbfb5697c
sizeof(struct TEST)=24

猜你喜欢

转载自blog.csdn.net/chenyefei/article/details/86508829