Understanding sizeof(char) in 32 bit C compilers

后端 未结 8 2341
情歌与酒
情歌与酒 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:51

    Because of optimisation padding is added so size of an object is 1, 2 or n*4 bytes (or something like that, talking about x86). That's why there is added padding to 5-byte object and to 1-byte not. Single char doesn't have to be padded, it can be allocated on 1 byte, we can store it on space allocated with malloc(1). st cannot be stored on space allocated with malloc(5) because when st struct is being copied whole 8 bytes are being copied.

提交回复
热议问题