size of struct and corresponding variable

前端 未结 6 1143
慢半拍i
慢半拍i 2021-01-14 07:53

If i define a char variable

char a;

and a structure with a single char member

struct OneChar {
char a;
};
<
6条回答
  •  一个人的身影
    2021-01-14 08:25

    When you take the size of a struct, it includes the padding bytes. But, if you have something like the following:

    void fun()
    {
        char c;
        int n;
    }
    

    The compiler is free to insert padding for whatever reason it sees. So, the padding could be there in case of standalone variables, you just can't know about that like in the case of a struct.

提交回复
热议问题