We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

Member functions

a_nicet
a_nicet asked
on
Medium Priority
288 Views
Last Modified: 2010-04-01
 The size of a c++ object is independent of the number of member functions. why ?
Comment
Watch Question

Commented:
because they are in some virtual function table
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Top Expert 2004
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Commented:
Just so you know (you probably already do anyway), the size of an object is equal to the sum of the sizes of its variables.  This has probably already been mentioned in one of the links posted above, but I did not read both fully :)

Commented:
Non-virtual member function usage is determined at compile time, and therefore there is no need for the object to carry the overhead of a function table. Adding more functions does not increase the object size.

If you have virtual functions, which function gets called gets determined at run-time, and the object maintains a table of function pointers to figure this out. In this case, the size of the object will increase as virtual functions are added because each new virtual function requires an entry in the function table.
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Top Expert 2004

Commented:
guntherothk,
> adding additional virtual functions only increases the size of the
> vtable, not the vtable pointer in each instance

Exactly. As instances of a class should have the very same methods, they share the very same virtual table.

Stefan
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Commented:
bronkbronk:

>> "due to compiler and platform specifics"

Does this refer to sizeof(int) on one platform not being the same as it is on others ?  Or are there more intricate compiler details that you would like to go into ?

One problem is alignment, i.e. the requirement on the address of data to be a multiple of a specific value (e.g., a multiple of 4 bytes on a 32 bit machine using an ordinary compiler). Try checking the size of these three classes on your favorite compiler:

class A { char c };
class B { int i; };
class C { char c; int i; };

Chances are you get something like 1(sic!), 4, and 8. The last case is because introducing the integer member also forces adaptation to the platform specific alignment for the entire class.

Regards,
Bronk

Commented:
bronkbronk:

Interesting stuff.  Many thanks !

Author

Commented:
The question  has no relation with the virtual functions and no vptr.  
like
class A { };   // Size of class is 1 bcoz 1 byte is an identity..
class B{ Print() {}; };  // The size is X
class C{PrintA() { };  Printb() { } ; };  // The size is X
if so where is memory for function is allocated.. How does the object knows that such function is calling ??
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
a_nicet,

There is a difference between classes and objects. Each class can define its own set of functions. This code is common to all objects of that class. The code is stored in your executable file. When starting the program the executable code is atored in memory.

During execution, you can create any number of objects. Creating an object means allocating memory for holding the objects own data. However, there is no need to duplicate any code to be executed, but only a reference to (i.e., the position in memory of) the code to code is associated with the object. In many cases, the relationship between an object and the code to be executed is resolved during compilation, and does thus not impact the size of the object. When using virtual functions, this is not the case, and thus each individual object having at least one virtual function will need to hold a reference to a virtual table. One and the same virtual table is however shared by all objects of the same class.

Let's look at your example above:

class A { };   // Size of class is 1 bcoz 1 byte is an identity..
class B{ Print() {}; };  // The size is X
class C{PrintA() { };  Printb() { } ; };  // The size is X

The classes B and C specify some code common to all objects of those classes. However, when instantiating an object of class B or class C, there is no need to duplicate the code for each object, but all objects execute the same code. Since neither of A, B or C has any object data nor any virtual functions, objects of all these classes should have the same size, i.e. 1.

You can also try to checking the sizes of the below classes:

class A { int i; };
class B { int i; void foo() {} };
class C { int i; virtual void foo() {} };

On a 32-bit machine you will likely get sizeof(A)=sizeof(B)=4 and sizeof(C)=2*sizeof(A)=8. Note that sizeof(AClass) returns the size of objects of the class, not the size of the code and data of class itself.

Thus, for
A m1;
B m2;
C m3;

you will get
sizeof(A) == sizeof(m1)
sizeof(B) == sizeof(m2)
sizeof(C) == sizeof(m3)

On a barely related issue, note that running sizeof on polymorphic objects can give you sursprising result;

class D : public C
{
  char c;
};
C* ptr = new D();

Will yield
sizeof(D) != sizeof(*ptr)


Regards,
/Bronk
> sizeof on polymorphic objects can give you sursprising result;

not really surprising, sizeof only looks at the static type of an object.  The static type of *ptr is 'C' so sizeof(*ptr) will always return the sizeof(C), regardless of whatever *ptr actually points at.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.