结构体 字节对齐的练习

Asura 2018年01月08日 101次浏览
  1. 分析结构体大小

    struct S1
    {
    char c1;
    double d;
    char c2;
    char c3;
    };
    struct S2
    {
    char c1;
    char c2;
    char c3;
    double d;
    };
    struct S3
    {
    char c1;
    S1 s;
    char c2;
    char c3;
    };

  1. 分析结构体大小

    #pragma pack(1)
    struct S1
    {
    char c1;
    double d;
    char c2;
    char c3;
    };
    #pragma pack()
    struct S3
    {
    char c1;
    S1 s;
    char c2;
    char c3;
    };

  1. 分析结构体大小

    #pragma pack(4)
    struct S1
    {
    char c1;
    double d;
    char c2;
    char c3;
    };
    #pragma pack()
    struct S3
    {
    char c1;
    S1 s;
    char c2;
    char c3;
    };

  1. 分析结构体大小

    struct S1
    {
    char c1;
    double d;
    char c2;
    char c3;
    };
    #pragma pack(1)
    struct S3
    {
    char c1;
    S1 s;
    char c2;
    char c3;
    };
    #pragma pack()