Understanding sizeof(char) in 32 bit C compilers

后端 未结 8 2333
情歌与酒
情歌与酒 2020-12-17 19:18

(sizeof) char always returns 1 in 32 bit GCC compiler.

But since the basic block size in 32 bit compiler is 4, How does char occup

8条回答
  •  鱼传尺愫
    2020-12-17 19:44

    Sample code demonstrating structure alignment:

    struct st 
    {
    int a;
    char c;
    };
    
    struct stb
    {
    int a;
    char c;
    char d;
    char e;
    char f;
    };
    
    struct stc
    {
    int a;
    char c;
    char d;
    char e;
    char f;
    char g;
    };
    
    std::cout<

    The size of the struct is bigger than the sum of its individual components, since it was set to be divisible by 4 bytes by the 32 bit compiler. These results may be different on different compilers, especially if they are on a 64 bit compiler.

提交回复
热议问题