用户定义的类的大小 [英] size of a user defined class

查看:94
本文介绍了用户定义的类的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么下面代码中的类 cl1 的大小为8而不是5,而 cl2 类的大小为1?

why the size of the class cl1 in the following code is 8 but not 5, while the size of class cl2 is 1?

class cl1 {                                                                                                                                                                               
public:                                                                                                                                                                                   
    int n;                                                                                                                                                                                
    char cb;                                                                                                                                                                              
    cl1();                                                                                                                                                                                
    ~cl1();                                                                                                                                                                               
};                                                                                                                                                                                        

class cl2 {                                                                                                                                                                               
public:                                                                                                                                                                                   
    char cb;                                                                                                                                                                              
    cl2();                                                                                                                                                                                
    ~cl2();                                                                                                                                                                               
};                                                                                                                                                                                        


推荐答案

编译器可以自由地在类成员,以确保变量正确对齐等。插入什么填充是由实现。在这种情况下,我猜想编译器在 cl1 :: cb 之后添加了3个字节的填充,也许是为了确保内存中的下一个变量在4 - 字节边界。

The compiler is free to insert padding in between and after class members in order to ensure that variables are properly aligned, etc. Exactly what padding is inserted is up to the implementation. In this case, I'd guess that the compiler is adding 3 bytes of padding after cl1::cb, perhaps to ensure that the next variable in memory is aligned on a 4-byte boundary.

这篇关于用户定义的类的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆