1
我有这个类:大小不匹配
class MyClass{
public:
shared_ptr<X> a;
shared_ptr<Y> b;
std::string c;
std::vector<double> d;
std::vector<shared_ptr<Z>> e;
int f;
};
,当我编译上VS2012与交换机看到客舱布局我得到这个:
1> class MyClass size(128):
1> +---
1> 0 | {vfptr}
1> 8 | [email protected]@@ a
1> 24 | [email protected]@@ b
1> 40 | [email protected][email protected]@[email protected]@[email protected]@[email protected] c
1> 72 | [email protected][email protected]@[email protected]@ d
1> 96 | [email protected][email protected]@@@[email protected]@[email protected][email protected]@@@[email protected]@@[email protected]@ e
1> 120 | f
1> | <alignment member> (size=4)
1> +---
它意味着数据成员c
是32个字节。
但是,如果我在我的平台(Win 7 64)上做sizeof(std::string)
,使用MSVC11,我会得到40个字节。
为什么sizeof()
给我40,但上面的编译器内存布局意味着32?
为什么当没有虚拟函数的类时会显示一个vtable指针? –
@MobyDisk我删除了专注于数据成员的方法。 – user997112
@ user997112:不要删除你认为不重要的东西。向我们展示您编译的* exact *代码以及您为该代码获得的* exact *输出。 –