开发者

c++ sizeof() of a class with functions

开发者 https://www.devze.com 2023-03-17 05:39 出处:网络
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 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 开发者_JAVA百科the size is 1 ? And is it compiler specific ?

Thanks! :-)


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.)


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)


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.


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.


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


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消