Why class size depend only on data members and not

2019-01-11 13:51发布

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

标签: c++ class size
7条回答
三岁会撩人
2楼-- · 2019-01-11 14:08

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.

查看更多
The star\"
3楼-- · 2019-01-11 14:18

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.

查看更多
干净又极端
4楼-- · 2019-01-11 14:21

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楼-- · 2019-01-11 14:24

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.

查看更多
我只想做你的唯一
6楼-- · 2019-01-11 14:27

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.

查看更多
啃猪蹄的小仙女
7楼-- · 2019-01-11 14:28

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

查看更多
登录 后发表回答