Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Is the vptr at the beginning of an object?

Reply
Thread Tools

Is the vptr at the beginning of an object?

 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      04-05-2010
* DaveB:
> Bo Persson wrote:
>> DaveB wrote:
>>> James Kanze wrote:
>>>> On Apr 3, 1:40 pm, "Alf P. Steinbach" <(E-Mail Removed)> wrote:
>>>>> * red floyd:
>>>>>> On 4/2/2010 2:12 PM, DaveB wrote:
>>>>>>> [insult to a member of the Committee redacted]
>>>>>> *PLONK*
>>>>> Well, there was a bit of insult, true.
>>>>> On the other hand, I fail to see what James' state as a member
>>>>> of the standardization committee has to do with anything.
>>>> I don't think it does (especially since I haven't been
>>>> particularly active lately). On the other hand, calling an
>>>> accurate and precise description of the issues "technical
>>>> gibberish" gives a very good idea of the competence (technical
>>>> and otherwise) of the poster. One gets the feeling that he
>>>> prefers to ignore real issues.
>>> The question remains open. Maybe someone NOT in the priesthood of
>>> technobabblery can answer it.

>> I think we have already seen the answer:
>>
>> In some cases it might be that a vptr is the first member of an
>> object. The only exception is when it is not, or when there isn't
>> exactly one vptr in the object.

>
> I didn't see that one, and it points the direction to the answer I was
> looking for. I was looking for someone who knows about a number of
> "mainstream" compilers and can expound on the implementations of vptrs in
> those. The assumption, or hope, being that "all" compilers use vptrs
> rather than some other way. I asked more generally than I needed to know,
> but the added information from someone who had it was easy enough to ask
> for. Particularly, though, I wanted to know about gcc because between
> that and VC, I my target platform is covered by those 2, and I am using
> PLATFORM loosely here. I don't use multiple inheritance and am not
> worried about anything strange happening in some remote corner case, and
> if I was, I would have given more information than just "where is that
> dang vptr!?". What I am doing that makes me need that information is not
> relevant, and none of anyone's business unless I volunteer it. For
> someone (not you in this post) to say that the question has been finely
> answered, while I still have not the information I solicited, is pompous,
> or at least seems so with a high degree of certainty, not to let a
> sleeping dog alone, of course.


What the real problem is, is relevant, believe or not.

For example, if you're trying to reinvent COM or XCOM, then you can leverage the
guarantees that the compilers that support COM provide. It's slightly off topic
in this group but hey. No problem, except that going into details about that
would be ungood if that's not what you're trying to do.

For another example, if you're trying to somehow dynamically change an object's
type, then we can suggest less braindead alternatives. Yes, it's braindead. I've
actually worked with people who tried that, although it was in Visual Basic.
They also did other very braindead things, such as one DLL per class, with lots
and lots of classes. Of course they only asked me for help *after* they'd done
these silly things, when the consequences had really started to bite them.


Cheers & hth.,

- Alf
 
Reply With Quote
 
 
 
 
DaveB
Guest
Posts: n/a
 
      04-05-2010

"Alf P. Steinbach" <(E-Mail Removed)> wrote in message
news:hpbri4$v19$(E-Mail Removed)-september.org...
>* DaveB:
>> Bo Persson wrote:
>>> DaveB wrote:
>>>> James Kanze wrote:
>>>>> On Apr 3, 1:40 pm, "Alf P. Steinbach" <(E-Mail Removed)> wrote:
>>>>>> * red floyd:
>>>>>>> On 4/2/2010 2:12 PM, DaveB wrote:
>>>>>>>> [insult to a member of the Committee redacted]
>>>>>>> *PLONK*
>>>>>> Well, there was a bit of insult, true.
>>>>>> On the other hand, I fail to see what James' state as a member
>>>>>> of the standardization committee has to do with anything.
>>>>> I don't think it does (especially since I haven't been
>>>>> particularly active lately). On the other hand, calling an
>>>>> accurate and precise description of the issues "technical
>>>>> gibberish" gives a very good idea of the competence (technical
>>>>> and otherwise) of the poster. One gets the feeling that he
>>>>> prefers to ignore real issues.
>>>> The question remains open. Maybe someone NOT in the priesthood of
>>>> technobabblery can answer it.
>>> I think we have already seen the answer:
>>>
>>> In some cases it might be that a vptr is the first member of an
>>> object. The only exception is when it is not, or when there isn't
>>> exactly one vptr in the object.

>>
>> I didn't see that one, and it points the direction to the answer I was
>> looking for. I was looking for someone who knows about a number of
>> "mainstream" compilers and can expound on the implementations of vptrs
>> in those. The assumption, or hope, being that "all" compilers use
>> vptrs rather than some other way. I asked more generally than I needed
>> to know, but the added information from someone who had it was easy
>> enough to ask for. Particularly, though, I wanted to know about gcc
>> because between that and VC, I my target platform is covered by those
>> 2, and I am using PLATFORM loosely here. I don't use multiple
>> inheritance and am not worried about anything strange happening in
>> some remote corner case, and if I was, I would have given more
>> information than just "where is that dang vptr!?". What I am doing
>> that makes me need that information is not relevant, and none of
>> anyone's business unless I volunteer it. For someone (not you in this
>> post) to say that the question has been finely answered, while I still
>> have not the information I solicited, is pompous, or at least seems so
>> with a high degree of certainty, not to let a sleeping dog alone, of
>> course.

>
> What the real problem is, is relevant, believe or not.


Another, and again, posting outside of the scope of the question. "Why
so, how to for"?! Do you know what "scope" means? No offense meant if you
don't know that, or the little that I do. I didn't ask for brain-dumps of
anyone's experience or realm of knowledge. I asked a specific question.
If anyone tries to add "more" or "other", well then we have what is now:
an unanswered question.


 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      04-05-2010
On 04/ 5/10 06:21 PM, DaveB wrote:
>
> Another, and again, posting outside of the scope of the question.


Then why don't you ask on a group or forum where it would get an
appropriate answer? You mention gcc and VC, they both have their own
groups.

--
Ian Collins
 
Reply With Quote
 
Peter Remmers
Guest
Posts: n/a
 
      04-05-2010
Am 02.04.2010 23:12, schrieb DaveB:
> "James Kanze"<(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> On Apr 2, 12:16 pm, "DaveB"<(E-Mail Removed)> wrote:
>>> James Kanze wrote:
>>> > On Apr 2, 9:56 am, "DaveB"<(E-Mail Removed)> wrote:
>>> >> In practice, across most compilers, is the vptr at offset 0
>>> >> of a class object? I've found some posts that say GCC puts
>>> >> it at the end, but they were very old posts and I don't use
>>> >> it now so can't test it.

>>
>>> > Who cares? What does it matter where the vptr is placed?
>>> > (And of course, many objects have more than one vptr, and
>>> > not all of them can be at offset 0.)

>>
>>> If you couldn't answer the question asked, you shouldn't have
>>> bothered posting!

>>
>> I gave the only relevant answer. It doesn't matter where the
>> compiler puts the vptr. It could put it anywhere, and change
>> where it puts it from one version to the next, or even with a
>> change in the compilation options. And of course, the question
>> doesn't even make sense, since objects may have more than one
>> vptr.
>>

>
> Techno gibberish. I asked about common practice, not hypothetical theory,
> and it doesn't take anyone with one ioda of intuition to assume that I
> was talking about the simple case. Use of this forum to spout
> technobabble at any/every opportunity, is annoying at best. (If you can't
> help it because you are a nerd, then I apologize for not being cognitive
> of the affliction, and hope you get the help you need).


I agree.

James wasn't able to come down from his noble steed and answer a simple
question. It is my obervation from reading this ng for quite some time
that this newsgroup is inhabitated by people who tend to indoctrinate
the casual poster without themselves having any clue about the
question's or the questioner's background. I use the word "indoctrinate"
in order to avoid using the word "smartass". It is all too tempting when
you are an "expert" to react with arrogance and presumtuousness to a
"non-expert" question.

I agree, and I have made the experience myself, that many questions
arise from problems that people would not have if they hadn't made a bad
decision at some earlier point, i.e. they are trying to tackle the
problem from the wrong side. You can see from the question that they are
on the wrong track, and the best way to help them is to point out some
root cause, which quite often has yet to be wormed out of them. I agree
that sometines the devil is in the detail, and I'm a perfectionist
myself who gets lost in the details all too often.

But it is just wrong alienate people by answering "who cares?", or "you
shouldn't be asking this question". Maybe there's a reason that you
don't see at first. Maybe it is just pure curiosity, which, if nothing
else, makes it a perfectly legitimate question (for an example, see Paul
N's post).

Even though James' answer does have a technical correctness to it, it is
actually avoiding the question by moving to a different level. The
question was "In practice, ...", and this can very well be answered with
an "In practice, ..." answer. It was very evident that the questioner
knows this is an implementation detail, and portable code should not
depend on it. Maybe even platform dependent code should not rely on it,
and there may be better ways. But, again, you don't know the intentions,
so drawing premature conclusions is inappropriate. Maybe answering the
question and *then* adding "... but what exactly is it that you want to
do?" would come across in a completely different light.

Maybe this question is compiler specific and should be asked in a
compiler specific newsgroup. Maybe the "across most compilers" part has
made it ok to be posted here. I'm sure this would not have been the
first time implementation details were discussed here.

Maybe James is a committee member. Maybe this should not have anything
to do with the way he answered the question, or the way the answer may
be taken.

Maybe Dave's reaction to James' answer was a bit harsh. Maybe he does
have a point.

So much for my two cents.

Regards,
Peter
 
Reply With Quote
 
Joshua Maurice
Guest
Posts: n/a
 
      04-05-2010
On Apr 3, 7:52*pm, "DaveB" <(E-Mail Removed)> wrote:
> James Kanze wrote:
> > On Apr 3, 1:40 pm, "Alf P. Steinbach" <(E-Mail Removed)> wrote:
> >> * red floyd:

>
> >>> On 4/2/2010 2:12 PM, DaveB wrote:
> >>>> [insult to a member of the Committee redacted]

>
> >>> *PLONK*

>
> >> Well, there was a bit of insult, true.

>
> >> On the other hand, I fail to see what James' state as a member
> >> of the standardization committee has to do with anything.

>
> > I don't think it does (especially since I haven't been
> > particularly active lately). *On the other hand, calling an
> > accurate and precise description of the issues "technical
> > gibberish" gives a very good idea of the competence (technical
> > and otherwise) of the poster. *One gets the feeling that he
> > prefers to ignore real issues.

>
> The question remains open. Maybe someone NOT in the priesthood of
> technobabblery can answer it.


Maybe you could pull your head out of your ass and listen. Your
question does not make sense, even if you're adding on "for most
commercial compilers in common use". All commercial C++ compilers
support multiple inheritance and virtual inheritance. Some object in
such a hierarchy may will multiple vptrs. Your question was "Is the
vptr always at offset 0 of the object?" My answer is the question
"Which vptr?"

Finally, perhaps you could explain the problem you're trying to solve.
That would significantly help us give you useful answers.
 
Reply With Quote
 
Joshua Maurice
Guest
Posts: n/a
 
      04-05-2010
On Apr 5, 3:19*am, Peter Remmers
<(E-Mail Removed)> wrote:
> Even though James' answer does have a technical correctness to it, it is
> actually avoiding the question by moving to a different level. The
> question was "In practice, ...", and this can very well be answered with
> an "In practice, ..." answer. It was very evident that the questioner
> knows this is an implementation detail, and portable code should not
> depend on it. Maybe even platform dependent code should not rely on it,
> and there may be better ways.


It is evident that he does not know what he's talking about, as he
does not know there can be multiple vptrs per object.

> But, again, you don't know the intentions,
> so drawing premature conclusions is inappropriate. Maybe answering the
> question and *then* adding "... but what exactly is it that you want to
> do?" would come across in a completely different light.


As he does not know what he's talking about, James gave the standard
line: "You probably don't need to do this, nor want to do this, nor
can do this portably. What are you trying to do? Perhaps there's a
sane way to do it without relying upon an implementation detail."

If he was simply asking about the implementation detail to learn about
the C++ language (which is a good thing), then that's a different
story. In which case, the answer is: "It doesn't matter. It could be
always at the start (for a single vptr per object), or whereever the
compiler feels like putting it. It does not affect the algorithm in
any meaningful way, though it will affect binary compatibility if the
compiler were to change where it put it, or if a base class, direct or
indirect, was added or removed, etc. etc. for anything that can break C
++ binary compatibility. As a practical matter, the compiler probably
puts the vptr at offset 0 for the simple cases, and has a more complex
solution for multiple and virtual inheritance."
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      04-05-2010
On Apr 5, 11:19 am, Peter Remmers
<(E-Mail Removed)> wrote:
> Am 02.04.2010 23:12, schrieb DaveB:
> > "James Kanze"<(E-Mail Removed)> wrote in message
> >news:(E-Mail Removed)...
> >> On Apr 2, 12:16 pm, "DaveB"<(E-Mail Removed)> wrote:
> >>> James Kanze wrote:
> >>> > On Apr 2, 9:56 am, "DaveB"<(E-Mail Removed)> wrote:
> >>> >> In practice, across most compilers, is the vptr at offset 0
> >>> >> of a class object? I've found some posts that say GCC puts
> >>> >> it at the end, but they were very old posts and I don't use
> >>> >> it now so can't test it.


> >>> > Who cares? What does it matter where the vptr is placed?
> >>> > (And of course, many objects have more than one vptr, and
> >>> > not all of them can be at offset 0.)


> >>> If you couldn't answer the question asked, you shouldn't have
> >>> bothered posting!


> >> I gave the only relevant answer. It doesn't matter where the
> >> compiler puts the vptr. It could put it anywhere, and change
> >> where it puts it from one version to the next, or even with a
> >> change in the compilation options. And of course, the question
> >> doesn't even make sense, since objects may have more than one
> >> vptr.


> > Techno gibberish. I asked about common practice, not
> > hypothetical theory, and it doesn't take anyone with one
> > ioda of intuition to assume that I was talking about the
> > simple case. Use of this forum to spout technobabble at
> > any/every opportunity, is annoying at best. (If you can't
> > help it because you are a nerd, then I apologize for not
> > being cognitive of the affliction, and hope you get the help
> > you need).


> I agree.
>
> James wasn't able to come down from his noble steed and answer
> a simple question.


Excuse me, but I did answer the question. The fact that the
correct answer to the question (i.e. the facts) doesn't happen
to please the original poster is a different issue; I can't do
anything about that, since the facts aren't likely to change.

[...]
> Maybe this question is compiler specific and should be asked
> in a compiler specific newsgroup.


It's not that compiler specific, since every compiler I know
does use multiple vptr, and none makes any guarantee with
regards to where the vptrs will be in the object.

--
James Kanze
 
Reply With Quote
 
Peter Remmers
Guest
Posts: n/a
 
      04-06-2010
Am 06.04.2010 01:06, schrieb James Kanze:
> On Apr 5, 11:19 am, Peter Remmers
> <(E-Mail Removed)> wrote:
>> James wasn't able to come down from his noble steed and answer
>> a simple question.

>
> Excuse me, but I did answer the question.


Excuse me, but no, you didn't answer the question. You avoided the
question and came with a blunt answer.

Answering a question for internal workings with "none of your business"
is just silly. You could always say that one need not know the internal
specifics of a walkman, a watch, a record player, you-name-it, in order
to use them. Yet as a kid I disassembled such things to see how they
work. Asking how things work is a valid question that can always be
answered by explaining the details. I wouldn't accept "who cares" as an
answer. Your insisting on the "fact" that such a question must not be
asked just shows how narrow-minded you are.

> The fact that the
> correct answer to the question (i.e. the facts) doesn't happen
> to please the original poster is a different issue; I can't do
> anything about that, since the facts aren't likely to change.


The fact (and I don't use quotes here because I agree) that one does not
need to know about vtable layout to write good software in C++ is not
equal to the "fact" that one must not, for whatever reason, be
interested in such details, and it does not justify avoiding the
question and claiming this is the only correct answer. Again, I must
point out that we still don't know anything about Dave's reasons for
asking. And even if he intends to use this knowledge to write platform
and compiler dependent code this does not justify the attitude you are
showing.

If your anwer is "who cares", it's not that you can't answer the
question at the expected level (because I'm sure you could), it's that
you don't *want* to answer it. And I agree that if you only have your
"who cares" to contribute, you really shouldn't have bothered posting.
In the end this attitude just shows that you are trying to indoctrinate
people, and, please, spare us your education.

It's the same kind of education that people show when they drive on the
highway on the leftmost lane at the exact speed limit only to prevent
others from driving faster.

Regards,
Peter
 
Reply With Quote
 
Peter Remmers
Guest
Posts: n/a
 
      04-06-2010
Am 06.04.2010 00:23, schrieb Joshua Maurice:
> It is evident that he does not know what he's talking about, as he
> does not know there can be multiple vptrs per object.


How can you tell? I am sure he knows this.

> As he does not know what he's talking about, James gave the standard
> line: "You probably don't need to do this, nor want to do this, nor
> can do this portably. What are you trying to do? Perhaps there's a
> sane way to do it without relying upon an implementation detail."


If James had used this exact wording, it would not have come across as
arrogant as it did, although this does not answer the question.

> If he was simply asking about the implementation detail to learn about
> the C++ language (which is a good thing), then that's a different
> story.


How can you tell that he did not? And even if you don't agree with the
reasons, how can you be so presumptuous and judge that he is not worth
getting a decent answer to his question?

> In which case, the answer is: "It doesn't matter. It could be
> always at the start (for a single vptr per object), or whereever the
> compiler feels like putting it. It does not affect the algorithm in
> any meaningful way, though it will affect binary compatibility if the
> compiler were to change where it put it, or if a base class, direct or
> indirect, was added or removed, etc. etc. for anything that can break C
> ++ binary compatibility.


Blah blah. I'm sure the poster knew all of this. I know that I do, and
yet I can imagine asking the OP's question.

> As a practical matter, the compiler probably
> puts the vptr at offset 0 for the simple cases, and has a more complex
> solution for multiple and virtual inheritance."


Here we go. Why, oh why, is it so hard to just answer the question at
the level it was asked, and instead go on beating around the bush,
trying to educate people? As I wrote in my first post, you can always go
on questioning the reasons *afterwards*.

Regards,
Peter
 
Reply With Quote
 
Joshua Maurice
Guest
Posts: n/a
 
      04-06-2010
On Apr 6, 3:13 am, Peter Remmers
<(E-Mail Removed)> wrote:
> Am 06.04.2010 00:23, schrieb Joshua Maurice:
> > In which case, the answer is: "It doesn't matter. It could be
> > always at the start (for a single vptr per object), or whereever the
> > compiler feels like putting it. It does not affect the algorithm in
> > any meaningful way, though it will affect binary compatibility if the
> > compiler were to change where it put it, or if a base class, direct or
> > indirect, was added or removed, etc. etc. for anything that can break C
> > ++ binary compatibility.

>
> Blah blah. I'm sure the poster knew all of this. I know that I do, and
> yet I can imagine asking the OP's question.


Really? So you would ask a question which implies a falsehood? The
question went "At what offset is the vptr located?" This implies that
there is at most a single vptr per object, which is false. Thus the
question is nonsensical.

The OP basically replied with "Obviously, I'm talking only about the
simple case." My reply would have been something along the lines of
"Obviously, the dog is on fire." (Slashdot humor.)


> > As a practical matter, the compiler probably
> > puts the vptr at offset 0 for the simple cases, and has a more complex
> > solution for multiple and virtual inheritance."

>
> Here we go. Why, oh why, is it so hard to just answer the question at
> the level it was asked, and instead go on beating around the bush,
> trying to educate people? As I wrote in my first post, you can always go
> on questioning the reasons *afterwards*.


Exactly what am I supposed to be doing if not "educating people" when
answering questions posted to a newsgroup? I'm confused. Communication
is more than simply relating words verbatim. Discussion by its very
nature is the transfer of information, aka education. Perhaps you mean
some specific colloquialism of "education", perhaps meaning "to
patronize" or something similar. In that case, James and myself were
not being patronizing.

Again as a practical matter, when I hear people ask questions like
"what offset is the vptr at?", simply answering the question does the
group and the person a disservice. It may suggest that it's reasonable
to use this knowledge when it's not. While this knowledge is not
"evil", it has no practical or educational benefit, and simply
offering it up will probably do more harm than good for a prospective C
++ programmer.

And it's not beating around the bush. Beating around the bush would be
saying "It's probably at offset 0 for simple cases, and it depends on
the implementation for multiple and/or virtual inheritance situations.
However, you shouldn't rely on that, and the exact offset doesn't
matter for comprehension of the compilation process."

The concise form (the opposite of "beating around the bush") is
"Doesn't matter. You cannot rely on a particular offset in practice,
and the compilation process is basically the same irrelevant of the
offset for the simple case."
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
index of string from beginning of line vs beginning of file Jesse B. Ruby 9 03-27-2010 04:04 PM
vptr Rahul C++ 3 11-04-2007 12:03 PM
A basic doubt on VPTR and VTABLES Subhransu Sahoo C++ 4 11-07-2006 12:42 PM
Explanation of Vptr and V-table pai C++ 4 10-18-2006 02:09 PM
What is Data Type of Vptr pointer Veeru C++ 7 09-15-2006 10:00 AM



Advertisments