0
votes

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.

1
Why do you wish to do this?Ed Heal
"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
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
Here's another of my bookmarks you might like then - it explains one layout standard for VDT and RTTI information: Itanium C++ ABI. If you want to "see what this pointer is" etc. poke around in gdb or whatever debugger you use....Tony Delroy

1 Answers

0
votes

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.