ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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
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
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.
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
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.
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.
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.
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.
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
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.