Why class size depend only on data members and not

2019-01-11 13:48发布

问题:

I want to know the detail description on the size of a class. I want to know if there is only data members & member function without any virtual keyword then why the class size depends only on data members. For an eg:

class A {
    int a;
public:
    int display() { 
    cout << "A=" << a << endl;
   }
};

When I check the sizeof(A) i found that it is 4 byte. Why it is so? Why member function has no effect on the size of class A?

Thanks

回答1:

Because the class's functions are not saved inside the object itself. Think of it in terms of C programming, every function of class A takes one secret parameter, the this pointer, so in actuality they are just functions with one extra parameter.

For example imagine it like this:

int display(A* thisptr)
{
   //do something
   printf("%d",thisptr->a); 
   return;
}

So the display function is saved as just a simple function with one extra parameter. The name though is mangled depending on the compiler.

I believe that different rules apply for virtual functions which would involve function pointers but since I am not sure maybe someone else can enlighten us on this matter.



回答2:

This is implementation dependent - it's not specified in the standard. However, you are right, non-virtual member functions (and even virtual functions after the first one) don't affect class size.

That's because it would use a lot of memory if every instance of the class would have pointers to all functions. And why would they? At runtime, the object knows what type it is, and it can call the appropriate function. And the same function is identical across instances, all that's different is the object it operates on, which is passed as a parameter under the hood.



回答3:

Functions/methods are stored as code, not as data. Just like one executable is stored as single file, and may be launched multiple times - and multiple instances of it would have different data. Similarly, a function is and executable code, and data you pass to it can be different (like different documents, for same word-processing software).

Executable code will not have any sizeof, as they don't take space on stack or heap, while program is running. It is stored itself in the executable image, and is loaded once by the OS, when you launch the program.



回答4:

Just like a regular C function, a C++ method is just an address in memory for the execution to jump to when called. The only difference is the first parameter, which is a pointer to the object from which you are calling the function.



回答5:

Except for hidden data members introduced to implement virtual functions and virtual inheritance, the instance size is solely determined by a class’s data members and base classes. From C++: Under the Hood (1994) In case you want to know more.



回答6:

Member functions are part of text segment of a process and objects are part of data segment of the process. So as objects are data only, it calculate size by adding all the sizes of data members of a class. Member functions are common for all the objects, it only differ by the first argument of the particular objects pointer (called this pointer) which is passed as the hidden pointer to every member function of the class.



回答7:

Because the function state leaves in the stack, and gets created / deleted upon function entering / exiting.

In the size of the object there are just the members that contributes to the storage of the object state. A function is just a piece of code that starts at a given point that does not depend on the particular object instance it refers to.

Think to

A a1, a2;

a1.a and a2.a are different, but a1.display() and a2.dispaly() is the same function code (think to int A::display()) as int display(A* this))



标签: c++ class size