我的类包含 1 个枚举、2 个整数和一个指针。它没有虚函数,但大小为 20 字节。为什么?
正如标题所示。我真的不想列出它包含的所有其他成员,但我惊讶地发现,唯一的非静态数据成员是 enum
, 2 of int
和一个指向其自身类型的指针,其 sizeof
应为 20。
它没有虚函数,我已将指针和 enum
测量为各 4 个字节。我应该更加努力地寻找其他成员吗?
当从文件读回时,我需要此信息来为其类型的 n 个对象分配缓冲区。
平台:bada,环境:gcc 和 Win 7 x64 中的 Eclipse。
enum blockParams {enum1, enum2, /**/};
class Block : public Object {
public:
int begin;
protected:
Block() : begin(-1), end(UNCLOSEDBLOCK) {}
//Last index
int end;
private:
blockParams1 params;
const Block *parentBlock;
//Lots and lots (~80) member functions and static data members.
}
As per the title. I don't really want to list all the other members it contains but I was suprised to find that given the only non-static data members were the enum
, 2 of int
and a pointer to its own type that its sizeof
should be 20.
It has no virtual functions and I've measured the pointer and enum
as 4 bytes each. Should I look harder for other members?
I need this information to allocate a buffer for n objects of its type when reading back in from a file.
Platform: bada, Environment: gcc and Eclipse in Win 7 x64.
enum blockParams {enum1, enum2, /**/};
class Block : public Object {
public:
int begin;
protected:
Block() : begin(-1), end(UNCLOSEDBLOCK) {}
//Last index
int end;
private:
blockParams1 params;
const Block *parentBlock;
//Lots and lots (~80) member functions and static data members.
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
编译器可以自由地在成员之间引入填充,以实现各种对齐要求。
许多编译器提供了一种不可移植的方法来更紧密地打包结构(例如 GCC 的 __attribute ((packed)) ),但很少有充分的理由使用它。
The compiler is free to introduce padding between members to achieve various alignment requirements.
Many compilers offer a non-portable way to pack the structure more tightly (e.g. GCC's
__attribute ((packed))
), but there's seldom a good reason to use this.当我看到一个名为
Object
的基类型时,我真的很怀疑没有虚函数的说法。When I see a base type named
Object
, I really doubt the claim that there are no virtual functions.如果正确使用 sizeof,则不需要知道结构的大小,编译器将为您正确计算缓冲区大小。
20 的大小可能包括编译器为提高效率而添加的填充。
If you use sizeof correctly, you do not need to know the size of the struct, the compiler will calculate your buffer size correctly for you.
The size of 20 probably includes padding the compiler adds for efficiency.
大小是各部分+填充的总和。
在对象为空的情况下运行此命令: 使用
包含虚拟析构函数的对象运行此命令
如果我们假设您平台上的指针为 4 个字节(并且您有带有虚拟函数的对象)。我希望看到:
The size is the sum of the parts + padding.
Running this with Object just being empty:
Running this with object containing a virtual destructor
If we assume pointers on your platform are 4 bytes (and you have Object with virtual functions). I would expect to see: