LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-19-2011, 03:08 AM   #1
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
C++: Understanding the vtable


For this code:

Code:
class B1
{
  public:  
     virtual void f1() {}  
};
     
class D : public B1
{
  public:
      void f1() {}
};
    
int main ()
{
  B1 *b1 = new B1();
  D  *d  = new D();
  
  return 0;
}
After compilation, the vtable I get is this:
Code:
    Vtable for B1
    B1::_ZTV2B1: 3u entries
    0     (int (*)(...))0
    8     (int (*)(...))(& _ZTI2B1)
    16    B1::f1
    
...   

    Vtable for D
    D::_ZTV1D: 3u entries
    0     (int (*)(...))0
    8     (int (*)(...))(& _ZTI1D)
    16    D::f1
I failed to understand what do the entries like (int (*)(...))0 correspond to. Of course it means something like, it is a function which returns an int and takes unlimited number of arguments, I don't understand anything further.
To which function does this function pointer correspond to? and how do you know that? Mine is a 64 bit machine.

The second function pointer has an address associated at end?? To whom does that correspond to?

Code:
gcc version 4.4.1 [gcc-4_4-branch revision 150839] (SUSE Linux)
People on an other forum pointed me to: http://refspecs.freestandards.org/cx...83.html#vtable, yet I am not able to understand, which function pointer points to f1()? Okay, meanwhile I'll study the link further, perhaps I'll understand it sooner.

Last edited by Aquarius_Girl; 04-19-2011 at 03:13 AM.
 
Old 04-19-2011, 03:34 AM   #2
JohnGraham
Member
 
Registered: Oct 2009
Posts: 467

Rep: Reputation: 139Reputation: 139
Quote:
Originally Posted by Anisha Kaul View Post
Code:
    Vtable for B1
    B1::_ZTV2B1: 3u entries
    0     (int (*)(...))0
    8     (int (*)(...))(& _ZTI2B1)
    16    B1::f1
I failed to understand what do the entries like (int (*)(...))0 correspond to. Of course it means something like, it is a function which returns an int and takes unlimited number of arguments, I don't understand anything further.
To which function does this function pointer correspond to? and how do you know that? Mine is a 64 bit machine.
I believe the first entry is the "offset to top" in the link you posted, and is so that you can find the top of the object from just a vtable. A value of 0 means that the top of the vtable is the top of the object - a value of (say) -32 would mean that the top of the object is 32 bytes above the top of the vtable.


Quote:
Originally Posted by Anisha Kaul View Post
The second function pointer has an address associated at end?? To whom does that correspond to?
"_ZTI2B1" is the (mangled) symbol name for the default constructor.


Quote:
Originally Posted by Anisha Kaul View Post
which function pointer points to f1()?
The entry B1::f1 in the vtable for class B1, and the entry for D::f1 in the vtable for class D, point to the relevant f1() for their classes. Note that they're both at the same offset within the vtable (16) - so that you don't need to know what class's vtable you're looking at, just that your class will provide a function pointer at offset 16 and that it will point to whatever f1() method that class wants you to use.

Last edited by JohnGraham; 04-19-2011 at 03:36 AM.
 
1 members found this post helpful.
Old 04-19-2011, 04:53 AM   #3
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731

Original Poster
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
Quote:
Originally Posted by JohnGraham View Post
I believe the first entry is the "offset to top" in the link you posted, and is so that you can find the top of the object from just a vtable.
Thanks for bothering, but why do we need to find the "top of the object"? I don't understand.
 
Old 04-19-2011, 05:32 AM   #4
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731

Original Poster
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
and secondly if we forget the vtable, then what does this mean in programming terms? (int ()(...))0
 
Old 04-19-2011, 08:09 AM   #5
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by Anisha Kaul View Post
and secondly if we forget the vtable, then what does this mean in programming terms? (int ()(...))0
I think/guess that 0 means NULL, i.e. pointer to no function yet.

What you actually have is rather
Code:
int (*)(...)
; I think http://publications.gbdirect.co.uk/c..._pointers.html gives an answer.
 
Old 04-19-2011, 08:51 AM   #6
SigTerm
Member
 
Registered: Dec 2009
Distribution: Slackware 12.2
Posts: 379

Rep: Reputation: 234Reputation: 234Reputation: 234
Quote:
Originally Posted by Anisha Kaul View Post
For this code:
vtable is implementation/compiler-dependant, so trying to "understand" it is a waste of your time. Plus, you haven't dealt with such interesting things as multiple inheritance, diamond inheritance, rtti, typeid, etc. Advice: avoid messing with vtable directly - if you need to access vtable directly, you're trying incorrect solution. Since it is compiler-specific, you'll learn nothing useful (nothing that can be applied on any c++ compiler on any platform) from it.

Quote:
Originally Posted by Anisha Kaul View Post
and secondly if we forget the vtable, then what does this mean in programming terms? (int ()(...))0
"null"(or zero) pointer cast into "function that returns int and takes variable number of arguments" type.
 
1 members found this post helpful.
Old 04-19-2011, 09:09 AM   #7
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731

Original Poster
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
Thanks, but I am not fiddling with vtable because it a solution to some problem, but because, I am trying to understand the dynamic binding (w.r.t virtual functions) in depth, the book relates to vtable every now and then, so I thought, understanding vtable might help.
 
Old 04-19-2011, 10:29 AM   #8
SigTerm
Member
 
Registered: Dec 2009
Distribution: Slackware 12.2
Posts: 379

Rep: Reputation: 234Reputation: 234Reputation: 234
Quote:
Originally Posted by Anisha Kaul View Post
Thanks, but I am not fiddling with vtable because it a solution to some problem, but because, I am trying to understand the dynamic binding (w.r.t virtual functions) in depth, the book relates to vtable every now and then, so I thought, understanding vtable might help.
IMO, it is a wrong approach. It is unknown how exactly compiler will implement virtual table. It is also unknown how many virtual tables are here (there may be more than one if class uses multiple inheritance with virtual methods). Unless you want to make a compiler or implement similar mechanism, the only thing you need to know that you can override virtual function in derived classes, and that new method will be called instead original automatically. It might make sense to know that typically such functions are implemented as a table of pointers "somewhere", but digging any further ("what function is stored here?", "why the last pointer is zero?") is pointless.
 
1 members found this post helpful.
Old 04-19-2011, 10:39 AM   #9
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by Anisha Kaul View Post
Thanks, but I am not fiddling with vtable because it a solution to some problem, but because, I am trying to understand the dynamic binding (w.r.t virtual functions) in depth, the book relates to vtable every now and then, so I thought, understanding vtable might help.
Incidentally yesterday my brain and eyes "scanned" 'C++ Standard - ANSI ISO IEC 14882 2003.pdf' - the items I was looking for were "virtual" and "table" - as separated entities. I found no reference to anything which might be 'vtable' - which makes sense.

I.e. as I understand it, 'vtable' is a commonly known/used (and simple and effective) way to implement virtual methods and inheritance, but in no way part of the standard. I.e. I think 'vtable' is quite implementation dependent.
 
1 members found this post helpful.
Old 04-19-2011, 11:07 AM   #10
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731

Original Poster
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
Quote:
Originally Posted by SigTerm View Post
It might make sense to know that typically such functions are implemented as a table of pointers "somewhere", but digging any further ("what function is stored here?", "why the last pointer is zero?") is pointless.
I somehow feel you are right.

Quote:
Originally Posted by Sergei Steshenko View Post
I.e. as I understand it, 'vtable' is a commonly known/used (and simple and effective) way to implement virtual methods and inheritance, but in no way part of the standard. I.e. I think 'vtable' is quite implementation dependent.
Perhaps that's the reason, a person on stackoverflow.com, with 32k+ reputation points asked me how did I generate that vtable! He must have been using a Windows based compiler.
 
Old 04-19-2011, 12:30 PM   #11
SigTerm
Member
 
Registered: Dec 2009
Distribution: Slackware 12.2
Posts: 379

Rep: Reputation: 234Reputation: 234Reputation: 234
Quote:
Originally Posted by Anisha Kaul View Post
32k+ reputation points asked me how did I generate that vtable! He must have been using a Windows based compiler.
32k rep doesn't necessarily means that the person is incredibly experienced. It might mean that the person has a LOT of free time, or that the person has been registered for a long time, or was lucky enough to make a good reply to a question that is being asked very frequently and frequently appears in searches. Plus, an expert in one field can be a newbie in another. SO's rep system is flawed - you can spend 30 minutes writing a detailed reply and get 3 upvotes, or throw one line of code on your lunch break and get two dozens of upvotes in next 5 minutes. SO doesn't reward smart replies or unusual fields of knowledge, it doesn't reward answering a really difficult questions. It rewards participating in "near flamewars", answering question in popular fields, answering a "popular" questions (even if they are incredibly dumb, boring, can be found in any programming book), babysitting newbies, and so on. A teenager that barely grasped C++ can easily grind for rep and get more rep than a very experienced programmer, so don't use SO reputation to measure person's skill/knowledge.
 
1 members found this post helpful.
Old 04-19-2011, 12:47 PM   #12
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731

Original Poster
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
Quote:
Originally Posted by SigTerm View Post
32k rep doesn't necessarily means that the person is incredibly experienced.
You shouldn't have bothered so much, I didn't really mean all that. I have been a part of the reputation system discussion here at LQ and I do understand all that pretty well, myself. But on a side note, I also do feel for some reason that those posters in stackoverflow, are extremely knowledgeable, but of course reputation system always does not show the true worth of a person. Like I said before, I have earned there 107 reputation points for asking classy questions I haven't answered anything there yet.

Last edited by Aquarius_Girl; 04-19-2011 at 01:00 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Overriding inherited member functions in C++ vtable? disruptive Programming 14 08-18-2010 06:52 AM
undefined reference to ' vtable for CData' bianchi Programming 4 12-29-2005 02:48 AM
VTable Program lucky6969b Linux - Software 0 12-02-2005 03:33 AM
contents of vtable and objdump utilty krsnachaitanya Programming 2 02-11-2005 07:57 PM
Vtable in QT3 hpcpg Programming 0 01-23-2004 09:00 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:56 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration