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?

 
 
DaveB
Guest
Posts: n/a
 
      04-02-2010
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.


 
Reply With Quote
 
 
 
 
James Kanze
Guest
Posts: n/a
 
      04-02-2010
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.)

--
James Kanze
 
Reply With Quote
 
 
 
 
Paul N
Guest
Posts: n/a
 
      04-02-2010
On 2 Apr, 11:07, James Kanze <(E-Mail Removed)> 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.)


Just out of curiosity, I thought the compiler was obliged to put the
first member at offset zero? If so (and it could well be I'm wrong
here...) then it must put the vptr (assuming there is one) somewhere
else?
 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      04-02-2010
* Paul N:
> On 2 Apr, 11:07, James Kanze <(E-Mail Removed)> 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.)

>
> Just out of curiosity, I thought the compiler was obliged to put the
> first member at offset zero?


Only for a POD. For a POD it's guaranteed by reinterpret_cast to suitable type
yielding a pointer to the first member (and vice versa).


> If so (and it could well be I'm wrong
> here...) then it must put the vptr (assuming there is one) somewhere
> else?


When the first data member is at offset 0 a vptr, if the implementation uses
that scheme (but all do), must be put somewhere else, yes.

Generally this is only relevant when considering binary layout compatibility.

However, the language supports compilers that put a vpointer at offset 0, e.g.
when introducing a virtual member function in a derived class, because given a
pointer to some class type object a static_cast up or down an inheritance chain
can change the pointer value as given by a conversion to void*.


Cheers & hth.,


- Alf
 
Reply With Quote
 
DaveB
Guest
Posts: n/a
 
      04-02-2010
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!


 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      04-02-2010
On 04/ 3/10 12:16 AM, DaveB 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!


He did answer the question!

--
Ian Collins
 
Reply With Quote
 
DaveB
Guest
Posts: n/a
 
      04-02-2010
Ian Collins wrote:
> On 04/ 3/10 12:16 AM, DaveB 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!

>
> He did answer the question!


He can speak for himself!


 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      04-02-2010
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.

--
James Kanze
 
Reply With Quote
 
red floyd
Guest
Posts: n/a
 
      04-02-2010
On Apr 2, 8:25*am, Branimir Maksimovic <(E-Mail Removed)> wrote:
> On Fri, 2 Apr 2010 08:02:20 -0700 (PDT)
>
>
>
> James Kanze <(E-Mail Removed)> wrote:
> > 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.

>
> > --
> > James Kanze

>
> Is there possibility for implementation not to use vptr
> at all? Eg implementation that would dispatch on 'if's'
> based on type? Eg where indirect function calls are expensive?
>


I can imagine a vptr-less implementation, where each virtual function
is implemented as a pointer within each object, rather than a vtbl
implementation, where such pointers are a per-class thing. So to talk
about the vptr at all is nonsense, especially since it isn't mandated.

If you want to discuss vptr specifics, you'd almost by definition have
to do it in a platform specific forum. And, as James has mentioned,
in the case of MI and a vptr implementation, you'll probably have
multiple
vptrs.


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

"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).


 
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