0

Every C++ object that has a virtual function has a vptr that points to a vtable. How can I see what this vptr is, and the contents it is point to? I understand this is compiler dependent and it could put vptr anywhere in the object memory space. But is there anyway I can find what it is?

Cheers.

madu
  • 5,232
  • 14
  • 56
  • 96
  • 1
    Why do you wish to do this? – Ed Heal Nov 28 '14 at 04:59
  • 1
    *"Every C++ object that has a virtual function has a vptr that points to a vtable."* - the C++ Standard itself doesn't dictate how virtual dispatch should be orchestrated, so that's true - to the best of my knowledge - of all the currently available C++ compilers and *likely* to remain so, but not a certainty, especially in e.g. simpler programs compiled in one translation unit where run-time use of virtual dispatch or RTTI isn't actually made. – Tony Delroy Nov 28 '14 at 05:09
  • 1
    You might want to read [this SO question on alternative implementations](http://stackoverflow.com/questions/4352032/alternative-virtual-mechanism-implementations) and [this explanation of how Visual C++ implements virtual dispatch](http://www.openrce.org/articles/files/jangrayhood.pdf) – Tony Delroy Nov 28 '14 at 05:12
  • Thank you. I understand that some compilers will choose not to do dynamic binding if the program doesn't need it. But my need is how to see what this pointer is and to see its contents, when vtpr/vtable exists. – madu Nov 28 '14 at 05:12
  • I don't have a specific need for this. This is purely for understanding C++ at the object level (want to confirm what is said in Lippmans "C++ Object Model" book) – madu Nov 28 '14 at 05:15
  • 2
    Here's another of my bookmarks you might like then - it explains one layout standard for VDT and RTTI information: [Itanium C++ ABI](http://mentorembedded.github.io/cxx-abi/abi.html#rtti). If you want to "see what this pointer is" etc. poke around in gdb or whatever debugger you use.... – Tony Delroy Nov 28 '14 at 05:57
  • Thanks TonyD. In Visual Studio debugging you can see the these contents on __vftable. Just wondering if there is an easier way to do this in g++/gdb – madu Nov 28 '14 at 06:15
  • @madu If you try to understand OOP by trying to understand the implementation, you are doing something significantly wrong: Try to understand *when* dynamic binding occurs, and *not how it is implemented*. Just assume it works as specified. – U. Windl Jun 02 '23 at 10:21

1 Answers1

1

In this specific case, C has one vtable and A and B have none. You can see this for yourself by out-of-lining C's member functions, so that the vtable will actually be emitted, and correcting the other compile errors: extern "C" int puts(const char *);

struct A { virtual void func_1() = 0; };
struct B { virtual void func_2() = 0; };

struct C : A, B
{
  void func_1();
  void func_2();
};

... compiling to an object file, and then looking at the symbols:

$ gcc -c test.cc
$ nm test.o | c++filt
                 U puts
0000000000000000 T C::func_1()
000000000000001a T C::func_2()
0000000000000033 T non-virtual thunk to C::func_2()
0000000000000000 V typeinfo for A
0000000000000000 V typeinfo for B
0000000000000000 V typeinfo for C
0000000000000000 V typeinfo name for A
0000000000000000 V typeinfo name for B
0000000000000000 V typeinfo name for C
0000000000000000 V vtable for C
                 U vtable for __cxxabiv1::__class_type_info
                 U vtable for __cxxabiv1::__vmi_class_type_info
    void C::func_1() { puts("func_1"); }
    void C::func_2() { puts("func_2"); }

By above steps you can find the content it points to.

ssg
  • 247
  • 2
  • 15