c++具有函数的类的 sizeof()

发布于 2024-11-18 06:18:31 字数 407 浏览 9 评论 0原文

我有一个 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 技术交流群。

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

发布评论

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

评论(7

汹涌人海 2024-11-25 06:18:31

该类不包含数据成员,因此它是空的。标准要求每个类至少有大小 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.)

呆° 2024-11-25 06:18:31

它的大小为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)

旧伤还要旧人安 2024-11-25 06:18:31

成员函数本质上与常规函数相同,它们只是获得一个隐藏的 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 appropriate this 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. if sizeof(a) were 0, 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 formally Derived contains an instance of Base 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.

睡美人的小仙女 2024-11-25 06:18:31

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.

飘逸的'云 2024-11-25 06:18:31

sizeof(char)==1 总是,因为 char 是一个字节,sizeof 返回一个数字
字节数。 (但是,一个字节不一定是八位。)

绝对正确。因此,出现了术语“八位字节”(以区分 恰好是 8 位的内容与更常用的术语“字节”)。

有关详细信息,请参阅 IEEE 1541:

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

sizeof(char)==1 always, because a char is a byte and sizeof returns a number
of bytes. (However, a byte is not necessarily exactly eight bits.)

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

稀香 2024-11-25 06:18:31

问:虚拟函数是否会占用每个对象的空间,从而增加对象的大小?

答:不会。虚拟函数越多,vtable 就越大。子类越多,vtable 就越多。如果类没有虚函数,则不需要 vtable 或(每个对象)vtable 指针。

但这些都不会影响“sizeof”。无论如何,函数本身占用固定的空间。

Q: Do virtual functions take space on a per-object basis and therefore increase the sizeof an object?

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.

鸵鸟症 2024-11-25 06:18:31

因为你的类是一个“引用变量”,并且根据 MSDN:“即使对于空类,sizeof 运算符也不会产生 0”。

例子:
<代码>

#include <stdio.h>

C级 { 民众: int f(int x, int y){ 返回 x; } int g(int x, int y){ 返回 x; } };

结构体 { 整数 f; 整数g; };

整数 main (int argc, char *argv[]) { objc; 对象; printf ("sizeof (c)= %d, sizeof (objc)= %d, sizeof (类 c)= %d...\n", sizeof (c)、sizeof (objc)、sizeof (c 类)); printf("sizeof(s)=%d,sizeof(objs)=%d,sizeof(struct s)=%d...\n", sizeof(s)、sizeof(objs)、sizeof(struct s)); 返回0; }

结果:

sizeof (c)= 1, sizeof (objc)= 1, sizeof (class c)= 1...
sizeof (s)= 8, sizeof (objs)= 8, sizeof (struct s)= 8...

还要注意“结构”和“类”之间的区别。

以下是更多信息:

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:

#include <stdio.h>

class c { public: int f(int x, int y){ return x; } int g(int x, int y){ return x; } };

struct s { int f; int g; };

int main (int argc, char *argv[]) { c objc; s objs; printf ("sizeof (c)= %d, sizeof (objc)= %d, sizeof (class c)= %d...\n", sizeof (c), sizeof (objc), sizeof (class c)); printf ("sizeof (s)= %d, sizeof (objs)= %d, sizeof (struct s)= %d...\n", sizeof (s), sizeof (objs), sizeof (struct s)); return 0; }

RESULT:

sizeof (c)= 1, sizeof (objc)= 1, sizeof (class c)= 1...
sizeof (s)= 8, sizeof (objs)= 8, sizeof (struct s)= 8...

Note, too, the difference between "struct" and "class".

Here's more info:

http://www.geekinterview.com/question_details/42847

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