Click here to Skip to main content
15,881,812 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am brushing up my understanding to C++ now and run some simple demos.
I tested a simple class and tried to find the size of it. it seems like only data members contribute into the size of class/object. where are the size of the member functions?
what is the rationale for not counting these members into size of class/object?

What I have tried:

here is my demo:
#include <iostream>
using namespace std;

class Box
{
public:
	double m_height;
	double m_width;
	double m_length;

	double volume()
	{
		double volume = 0;
		volume =m_height * m_width*m_length;
		return volume;
	}

	Box()
	{
		m_height = 0;
		m_width = 0;
		m_length = 0;
		cout << "inside constructor:" << endl;
	}
};

int main()
{
	Box myBox;

	//cout << "Box attribute m_height:" << myBox.m_height;

	cout << "size of Box:" << sizeof(Box) << endl;
	cout << "size of myBox instance :" << sizeof myBox << endl;

	return 0;
}


I got this on my screen output:
inside constructor:
size of Box:24
size of myBox instance :24
Posted
Updated 17-Oct-20 17:41pm

Because they are effectively static data - not in the formal sense of the static keyword, but in the sense of "one copy is shared by all instances". There is no point at all in duplicating the code in every instance generated since it can't be changed once compiled anyway!
 
Share this answer
 
Comments
Southmountain 17-Oct-20 17:14pm    
Thanks OG!
The sizeof operator pertains solely to size of the data. This is because when you copy an object only data is copied. Code does not need to be copied so it is not included in the total. This is due to the nature of how a __thiscall function is invoked. The term refers to how a method of a class is called - the object's "this" pointer is implicitly passed to the method. In the Visual Studio X86 compilers it was passed in the ECX register but that is implementation-specific. What all of this means is c++ code does not move. It is placed in memory by the OS's loader and when one calls a method the location of the object's data is passed to the method. That means data can be moved around and dynamically allocated but the location of the code that operates on it does not change so the code does not need to be moved or accounted for when an object is copied or created.
 
Share this answer
 
Comments
Southmountain 17-Oct-20 15:47pm    
crystal clear to me! thank you!
If a class has any virtual functions that it, or another class, overrides, the class's first member is a pointer that precedes its other data members. This pointer (often called a vptr) references a table of virtual functions (often called a vtbl) associated with that class. Each class that inherits (and also the one that defines) any of those virtual functions will also have its own vtbl. The entries in a vtbl are function pointers that reference the appropriate implementation of each virtual function for that particular class. So what happens "under the hood" when you invoke obj->func(args) is
auto vtbl = obj->vptr;
vtbl[index of func](obj, args); // obj is "this"
The index of func is hard-coded by the compiler, which also creates each vtbl, after which the linker gathers up all this stuff so that the loader can put it in memory when the program runs.
 
Share this answer
 
v3
Comments
Southmountain 17-Oct-20 17:13pm    
thanks for explaining this vtbl thing! I recall it now...
Quote:
I tested a simple class and tried to find the size of it. it seems like only data members contribute into the size of class/object. where are the size of the member functions?

Yes, you found it. An object (instance of a class) contains only the data members.
The reason why code of member function is not duplicated is because it stay the same for all instances of this class/object and the compiler know that.
Quote:
what is the rationale for not counting these members into size of class/object?

No matter the number of instances of same object, there is only 1 copy of the member functions.
 
Share this answer
 
Comments
Southmountain 18-Oct-20 18:53pm    
thank you!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900