Class size with pointer 'this'

The size of the class without data elements is returned as 1 byte, although there is a declared implicit pointer 'this'. Should the size of the return value be 4 bytes (on a 32-bit machine)? I came across articles that indicated that the 'this' pointer is not counted to calculate the size of the object. But I can not understand the reason for this. In addition, if any member function is declared virtual, the class size is now returned as 4 bytes. This means that vptr is counted to calculate the size of the object. Why are the vptr in question and the 'this' pointer ignored to calculate the size of the object?

+7
c ++ sizeof
source share
4 answers

The this pointer is not a member of the class. This is just a construct that is used in methods belonging to the class to access the current instance.

If you have a class like this:

 class IntPair { public: IntPair(int a, int b) : _a(a), _b(b) { } int sum() const { return _a + _b; } public: int _a; int _b; }; 

This class only requires space for two int instances for each instance. After you create an instance and use the sum() method, this method is called with a pointer to the instance, but this pointer always comes from somewhere else, it is not stored in the object instance.

For example:

 IntPair *fib12 = new IntPair(89, 144); cout << fib12->sum(); 

Note that the variable that becomes the this pointer is stored outside the object, in the area that created it.

You could actually convert a method similar to the above:

 static int sum2(const IntPair* instance) { return instance->_a + instance->_b; } 

If the above is defined inside the class (so that it can access private members), there is no difference. In fact, this is exactly how the methods are implemented behind the scenes; the this pointer is just a hidden argument for all member methods.

Call:

 IntPair* fib12 = new IntPair(89, 144); cout << IntPair::sum2(fib12); 
+6
source share

'this' is not saved as a data element in the class, but simply a "pointer" to an instance of the class. Consider this as a "hidden argument" passed to the method. In fact, on Win32 systems it is often passed in the ecx register (not eax, as I originally thought).

Once you have 1 or more virtual methods, your application needs a way to store pointers to virtual methods. This is called vtable, which is identical for all instances of the same class. Since you need to know at runtime which the "explicit" method calls for the "virtual method", the pointer to the vtable is stored in an instance of the class. Therefore, a vtable-pointer (or vptr) needs 4 bytes (or 8 bytes in a 64-bit system).

+4
source share

This pointer is not stored inside the object. There is no need to do this. You already have a pointer or object to call functions. As for size 1, the C ++ standard requires object objects to have different addresses.

+2
source share

The size of the pointer is always the size of the pointer that you want to keep in memory.

For example, if the memory address for int is 32-bit in a 64-bit architecture, then

int a = 10; int * b = & a; sizeof (b); // 32 sizeof (& b); 64

-one
source share

All Articles