VTable is same for all objects of class. That means it is stored in memory . Can we obtain the address of VTable without creating any object of class?

Showing Answers 1 - 27 of 27 Answers

DVS

  • Sep 12th, 2006
 

the address of the vtable is stored in the first four bytes of every object of the class. Access the this pointer and get its value.

#include <stdio.h>

class a
{
public:
 int x;
 virtual void fn()
 {
  printf("n%dn",(*this));
  printf("n%dn",(&this->x));
 }
};

void main()
{

 a obj;
 a obj2;
 obj.fn();
 obj2.fn();

}

  Was this answer useful?  Yes

Paulson Paul Chambakottukudyil

  • Sep 15th, 2006
 

The answer is correct, but not the given code. We can't use *this inside a member function.

  Was this answer useful?  Yes

Rakesh chilumuru

  • Sep 21st, 2006
 

But we are not supposed to create an object for the class...

  Was this answer useful?  Yes

Bhima Rao

  • Sep 26th, 2006
 

The Answer and the code are perfect.

  Was this answer useful?  Yes

anyone

  • Oct 14th, 2006
 

the answer is wrong, did you read the question at all? it says "Can we obtain the address of VTable __without__ creating any object of class?"

  Was this answer useful?  Yes

DVS is very much right.

I am giving following example , u can observe multiple pointer type instance of same class having different Object address (this) but same VTable address. Again I have added static int K , which address is same in all the instances of that class like VTable, thats means VTable is static variable which is added by Compiler at compile time.

/** CODING By MOHIT GONDULEY mohit.gonduley@gmail.com ***/

#include "stdio.h"

class VClass
{
 public:
 static int K;
 virtual void getVTableAddress(void)
 {
  printf("nObject Address :- %u AND VTABLE Address = %u ,static K Addres = %u n",this, *this,&K);
 }
};

int VClass::K = 0;

int main(int argc, char* argv[])
{
 VClass * VT1   = new VClass();
 VClass * VT2   = new VClass();
 VClass * VT3   = new VClass();

 VT1->getVTableAddress(); delete VT1;
 VT2->getVTableAddress(); delete VT2;
 VT3->getVTableAddress(); delete VT3;
 
 return 0;
}

Any suggestion pls write here or me @my mail address

  Was this answer useful?  Yes

Indrajit

  • Jul 22nd, 2007
 

Hi Mohit,

DVS is not right and neither are you as per as the exact question goes since it specifically asks not to instantiate the class for finding out the VTable address.

I would really like to know this, that if VTable is at all created if a class with Virtual function is not instantiated?

Also in DVS's code, in the printf ("n%dn",(&this->x));, could we not use &x directly without having to write &this->x? and can someone pls. explain what the leading and the ending n's imply in the printf format argument?

-Regards/Indrajit

  Was this answer useful?  Yes

Indrajit

  • Jul 22nd, 2007
 

Could anyone confirm or nullify the assertion that *this cannot be used inside a member function?

As per as I know this pointer and it's usage only makes sense inside a member function and not outside the scope of the object. In that case why sould *this be not useable?

  Was this answer useful?  Yes

hi Indrajit,

Sorry for late reply.
As u wrote in ur last post "I am wrong" , so I wanna make u sure that in last mail I was supporting DVS's statement  "the address of the vtable is stored in the first four bytes of every object of the class. Access the this pointer and get its value"
and that is true. I wasn't claiming the code which I had given, can derive address of VTable without creating object of that , nor it is possiple in standard ansi C++ code
[if u r not using debuger or memory specific api] , bcoz VTABLE implementation is
 compiler specific, varies from compiler to compiler, still if u have any code pls share
with us , I will be grateful abt u. so may u tell us where I am wrong.

your second query "if VTable is at all created if a class with Virtual function is not instantiated?"
so my answer is YES. thats why microsoft has introduced concept of MESSAGE_MAP
in vc++ prgramming to avaoid excess creation of VTABLE without any creation use of
object of class.

Again ur one more query
"Also in DVS's code, in the printf ("n%dn",(&this->x));, could we not use &x directly without having to write &this->x?"

So my answer is YES we can write in both ways , but here DVS might have been 
thought to write  &this->x to make code easily understandable to readers that x is member variable of class a.

ur last query "explain what the leading and the ending n's imply in the printf format argument?"
so google can give u better answer.

Best Regards,
Mohit




  Was this answer useful?  Yes

indrajit_p

  • Aug 24th, 2007
 

Hi Mohit,

I did not mean that the code you wrote is wrong or that it does not do what it says it does when I said you were wrong. I you notice, I said you are wrong as far as answering the original question goes because I thought by giving the code example you were trying to answer, "how we can get the Vtable address without intantiating the class?".

BTW, is VTable is a static member of a class with virtual functions or with any of it's base having virtual function then is there no hidden keyword like <classname>::vptr or something like it thru which the address could be retrieved?

As far as the leading and the ending "n"s in the printf format argument goes, I don't know what to put as the google search term and I personally think you and DVS meant /n instead of just n because with no backslash I think it would just print an n at the beginnig and end of each line ( I don't have access to a C/C++ compiler at the moment, so cannot verify this).

Please do correct me if I am wrong.
Indrajit

  Was this answer useful?  Yes

Prasad2008

  • Jun 10th, 2008
 

No it is not possible ,because VTABLE of the specific class gets loaded into memory only when an object containing a virtual function gets created.

  Was this answer useful?  Yes

wael.salman

  • Jul 30th, 2008
 

When an object is created, a pointer to this vtable, called the virtual table pointer or vpointer, is added as a hidden member of this object (often the first member). The compiler also generates "hidden" code in the constructor of each class to initialize the vpointers of its objects to the address of the corresponding vtable.

Make sure to understand that the vpointer  is created just when we create an object of the class.

So the asnwer is no.

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions