为什么我的对象占用 64 字节而不是 32 字节?

发布于 2024-08-11 06:19:29 字数 619 浏览 2 评论 0原文

我有一个像这样的类:

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-in​​ternal_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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

向日葵 2024-08-18 06:19:29

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

歌入人心 2024-08-18 06:19:29

如果你的类中有虚函数,由于虚函数表指针,它会使你的对象比内部类型更大。

顺便说一句,“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 : "

花期渐远 2024-08-18 06:19:29

我想您将为您的对象定义一个vtable指针。因为您启用了 RTTI,或者任何虚拟方法(包括析构函数)都会添加 vtable

http://en.wikipedia.org/wiki/Virtual_method_table

通常,编译器为每个类创建一个单独的 vtable。创建对象时,指向此 vtable 的指针(称为虚拟表指针或 vpointer)将作为该对象的隐藏成员添加(成为其第一个成员,除非它成为最后一个成员[2])。编译器还在每个类的构造函数中生成“隐藏”代码,以将其对象的 vpointers 初始化为相应 vtable 的地址。

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 a vtable

http://en.wikipedia.org/wiki/Virtual_method_table

Typically, the compiler creates a separate vtable for each class. When an object is created, a pointer to this vtable, called the virtual table pointer or vpointer, is added as a hidden member of this object (becoming its first member unless it's made the last[2]). The compiler also generates "hidden" code in the constructor of each class to initialize the vpointers of its objects to the address of the corresponding vtable.

猫卆 2024-08-18 06:19:29

我刚刚编译

#include <xmmintrin.h>
#include <iostream>

struct myType
{
    union {
        float data[4];
        __v4sf v;
    };
    typedef union {
        float data[4];
        __v4sf v;
    } myType_internal_t;
};

int main() {
    std::cout << sizeof(myType) << std::endl;
    std::cout << sizeof(myType::myType_internal_t) << std::endl;
}

结果是:

16
16

在 64 位 arch 上使用 gcc。正如预期的那样。 “__v4sf”是由四个浮点数组成的向量类型。所以问题可能出在你的“other_vector_4_type”中。也许它是多态的、虚拟的并且包含两个虚函数表?您在“myType”中使用了更多虚拟函数,这进一步增加了其大小?
该问题也可能与您的编译器或体系结构有关。

I just compiled

#include <xmmintrin.h>
#include <iostream>

struct myType
{
    union {
        float data[4];
        __v4sf v;
    };
    typedef union {
        float data[4];
        __v4sf v;
    } myType_internal_t;
};

int main() {
    std::cout << sizeof(myType) << std::endl;
    std::cout << sizeof(myType::myType_internal_t) << std::endl;
}

And the result is:

16
16

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文