为什么我的对象占用 64 字节而不是 32 字节?
我有一个像这样的类:
class myType
{
union {
float data[4];
other_vector4_type v
} ;
typedef union {
float data[4];
other_vector4_type v
} myType-internal_t;
<various member functions, operator overloads>
}
现在我想在我的顶点缓冲区中使用这种类型,但是 sizeof()
不符合预期。我将类对齐到 16 字节。
sizeof(myType)
产生 64。
sizeof(myType::myType-internal_t)
产生 32。
我读过很多关于数据对齐的文章,但我不知道我正在使用额外数据的地方。我尝试删除自定义构造函数,但它保持不变,将 class 关键字替换为 struct 也不会改变它(我不明白它的用途,因为它发生了!)
这很烦人,我现在将使用内部类型,因为我不会经常接触数据,但如果课程能够像我想要的那样工作那就太好了。
I have a class that goes like this:
class myType
{
union {
float data[4];
other_vector4_type v
} ;
typedef union {
float data[4];
other_vector4_type v
} myType-internal_t;
<various member functions, operator overloads>
}
Now I want to use this type in my vertex buffer, but the sizeof()
isn't as expected. I aligned the class to 16 bytes.
sizeof(myType)
yields 64.
sizeof(myType::myType-internal_t)
yields 32.
I've read quite a few articles on data alignment but I don't know where I'm using extra data. I tried stripping out the custom constructor but it remains the same, swapping the class keyword for struct
doesn't change it either (I don't understand what it's for, as it happens!)
This is annoying, I'll use the internal type for now since I won't be touching the data often, but it would be great to have the class work like I want.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
C++ 编译器可以将该结构设为 128 字节,以满足其所有需要。 C++ 标准未定义结构体/联合体的大小。但是,大多数编译器都有一个非标准 __PACKED 属性,您可以将其添加到结构的声明中,并且它将仅占用所需的确切字节数
A C++ compiler can make that struct 128 bytes for all it cares. The size of structs/unions is not defined by the C++ standard. However, most compilers have a nonstandard __PACKED attribute you can add to the declaration of a struct and it will take up only the exact amount of bytes that it needs
如果你的类中有虚函数,由于虚函数表指针,它会使你的对象比内部类型更大。
顺便说一句,“struct {”与“class { public :”是一样的
If you have virtual functions in your class it will make your objects bigger than the internal type because of the vtable pointer.
BTW "struct { " is just the same as saying "class { public : "
我想您将为您的对象定义一个
vtable
指针。因为您启用了 RTTI,或者任何虚拟方法(包括析构函数)都会添加vtable
http://en.wikipedia.org/wiki/Virtual_method_table
I imagine you will have a
vtable
pointer defined for your objects. Either because you have RTTI enabled or any virtual method (including the destructor) will add avtable
http://en.wikipedia.org/wiki/Virtual_method_table
我刚刚编译
结果是:
在 64 位 arch 上使用 gcc。正如预期的那样。 “__v4sf”是由四个浮点数组成的向量类型。所以问题可能出在你的“other_vector_4_type”中。也许它是多态的、虚拟的并且包含两个虚函数表?您在“myType”中使用了更多虚拟函数,这进一步增加了其大小?
该问题也可能与您的编译器或体系结构有关。
I just compiled
And the result is:
using gcc on a 64 bit arch. As expected. "__v4sf" is a vector type consisting of four floats. So the problem is probably in your "other_vector_4_type". Maybe it is polymorphic and virtual and contains two vtables? And you are using more virtual functions in "myType" which increase its size further?
The problem may be related to your compiler or the architecture too.