c++具有函数的类的 sizeof()
我有一个 C++ 问题。 我编写了以下类:
class c
{
int f(int x, int y){ return x; }
};
类 c 的 sizeof() 返回“1”。 我真的不明白为什么它返回 1。
为了更好地理解发生了什么,我添加了另一个函数:
class c
{
int f(int x, int y){ return x; }
int g(int x, int y){ return x; }
};
现在下面的内容真的让我很困惑! sizeof(c) 仍然是 1 (!?!?!?!)。 所以我猜函数不会改变类的大小,但为什么???为什么大小是 1 ?它是编译器特定的吗?
谢谢! :-)
I have a C++ question.
I wrote the following class:
class c
{
int f(int x, int y){ return x; }
};
the sizeof() of class c returns "1".
I I really don't understand why it returns 1.
Trying to understand better what is going on, I added another function:
class c
{
int f(int x, int y){ return x; }
int g(int x, int y){ return x; }
};
Now the following really got me confused! sizeof(c) is still 1 (!?!?!?!).
So I guess that functions doesn't change the size of the class, but why??? and why does the size is 1 ? And is it compiler specific ?
Thanks! :-)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
该类不包含数据成员,因此它是空的。标准要求每个类至少有大小 1,所以这就是您得到的。 (成员函数实际上并不在类的“内部”,它们实际上只是带有隐藏参数、命名空间和访问控制的自由函数。)
The class contains no data members, so it's empty. The standard demands that every class have at least size 1, so that's what you get. (Member functions aren't physically "inside" a class, they're really just free functions with a hidden argument and a namespace and access control.)
它的大小为1,因为它不能为0,否则该类型的两个对象将无法寻址(无法区分它们的地址)
It's size is of 1, because it can not be 0, otherwise two objects of this type wouldn't be addressable (couldn't differentiate their addresses)
成员函数本质上与常规函数相同,它们只是获得一个隐藏的
this
参数。因此,给定类型的每个实例不需要携带其成员函数的副本;编译器只跟踪常规函数,并为您提供适当的this
参数。因此,无论给定类型有多少功能,都不需要更改其大小。当您使用虚函数等进行复杂的继承时,情况会略有变化,但最终函数的数量仍然对对象的最终大小没有影响。初始大小为一个字节是因为所有对象都必须占用一些空间,这样可以保证没有两个对象占用相同的空间。考虑一个数组...
a[5]
与*(a + 5)
相同,添加到指针会增加对象大小的内存地址。如果sizeof(a)
为0
,则数组的所有元素都将折叠到同一地址。某些空间的对象类型是由标准强制规定的……大小恰好等于一则不是。
sizeof(c)
在你的情况下可能是 23,但没有理由这样做。为了完整性,子对象的大小可以为零。空基优化允许基类在不需要时不占用任何实际内存。因此,
sizeof(Base) == sizeof(Derived)
可能为真,即使在形式上Derived
包含隐藏在其中的Base
实例。这是标准允许的,但不是强制性的……例如,MSVC 在某些情况下不会使用它。Member functions are, essentially, the same as regular functions, they just get a hidden
this
paramter. So each instance of a given type does not need to carry around copies of its member functions; the compiler just keeps track of the regular functions, and provides an appropriatethis
paramter for you. So no matter how many functions a given type has, there is no need for its size to change. When you get into complicated inheritance with virtual functions and whatnot, this changes slightly, but in the end the number of functions continues to have no impact on the final size of the object.The initial size of one byte is because all objects have to occupy some space, so that you can be guarenteed no two objects occupy the same space. Consider an array...
a[5]
is the same as*(a + 5)
, and adding to a pointer increases the memory address by the size of the object. ifsizeof(a)
were0
, then all the elements of the array would collapse down to the same address.That the objects type of some space is mandated by the standard... that the size be equal to exactly one is not.
sizeof(c)
in your case could be 23, but there's no reason for it.For completeness, it is possible for a sub-object to have a size of zero. The empty base optimization allows for a base class to not occupy any actual memory if it does not need to. So
sizeof(Base) == sizeof(Derived)
might be true, even though formallyDerived
contains an instance ofBase
hidden inside it. This is allowed by the standard, but not mandated by it... MSVC, for instance, does not make use of it in some situations.1 表示 1 个字节。原因是方法不存储在对象中。它们由对象使用,但不存储在其中。只有类成员存储在对象中。尝试添加一个普通的
int
成员或其他内容,看看会发生什么。1 means 1 byte. And the reson is that methods are not stored within an object. They are used by objects, but not stored in them. Only class members are stored in objects. Try to add a plain
int
member or something and see what happens.绝对正确。因此,出现了术语“八位字节”(以区分 恰好是 8 位的内容与更常用的术语“字节”)。
有关详细信息,请参阅 IEEE 1541:
http://en.wikipedia.org/wiki/IEEE_1541
Absolutely true. Hence the term "octet" (to distinguish something that is exactly 8 bits from the more commonly used term "byte").
For more info, look at IEEE 1541:
http://en.wikipedia.org/wiki/IEEE_1541
答:不会。虚拟函数越多,vtable 就越大。子类越多,vtable 就越多。如果类没有虚函数,则不需要 vtable 或(每个对象)vtable 指针。
但这些都不会影响“sizeof”。无论如何,函数本身占用固定的空间。
A: No. The more virtual functions, the larger the vtable. The the more subclasses, the more vtables. If a class has no virtual functions, then there's no need for either a vtable or a (per-object) vtable pointer.
But none of this affects "sizeof". The functions themselves take a fixed amount of space, regardless.
因为你的类是一个“引用变量”,并且根据 MSDN:“即使对于空类,sizeof 运算符也不会产生 0”。
例子:
<代码>
结果:
还要注意“结构”和“类”之间的区别。
以下是更多信息:
http://www.geekinterview.com/question_details/42847
Because your class is a "reference variable" and, per MSDN: "The sizeof operator never yields 0 even for an empty class."
EXAMPLE:
RESULT:
Note, too, the difference between "struct" and "class".
Here's more info:
http://www.geekinterview.com/question_details/42847