address of base class and derived class different 
Author Message
 address of base class and derived class different

when a base class does not have a virtual member, but the
derived class does, the MSVC++ compiler 'slices' the vptr
table from the object, when referencing the derived object
through a pointer to the base class object.

Linux GNU does not do this: there the address/pointer is
the same in both cases.

What does the C++ standard say about this? And if it is
implementation dependent, how do you find out what address
to provide to delete()?

Sample code:

#include <iostream>
using namespace std;

class B
{
public:
    int i;

Quote:
};

class D: public B
{
public:
    virtual ~D(){}

Quote:
};

void doSomething( B * pb )
{
    cout << hex << pb << endl;

Quote:
}

void main()
{
    D * pd = new D;
    cout << hex << pd << endl;
    doSomething( pd );

Quote:
}

Results in my system in:

00301E60
00301E64



Tue, 02 Mar 2004 07:32:25 GMT  
 address of base class and derived class different
If you change the Class B to
class B
{
public:
    virtual void fun1(){};
    int i;

Quote:
};

The output will be the same.

yhhuang
ICQ:91074870



Tue, 02 Mar 2004 15:02:53 GMT  
 address of base class and derived class different

Quote:
> when a base class does not have a virtual member, but the
> derived class does, the MSVC++ compiler 'slices' the vptr
> table from the object, when referencing the derived object
> through a pointer to the base class object.

> Linux GNU does not do this: there the address/pointer is
> the same in both cases.

> What does the C++ standard say about this? And if it is
> implementation dependent, how do you find out what address
> to provide to delete()?

I'm a C++ newbie, but don't you need to provide a virtual
destructor in your base class anyway so you can use
delete with a base pointer?


Tue, 02 Mar 2004 21:34:18 GMT  
 address of base class and derived class different

Quote:

> when a base class does not have a virtual member, but the
> derived class does, the MSVC++ compiler 'slices' the vptr
> table from the object, when referencing the derived object
> through a pointer to the base class object.

> Linux GNU does not do this: there the address/pointer is
> the same in both cases.

> What does the C++ standard say about this? And if it is
> implementation dependent, how do you find out what address
> to provide to delete()?

The standard doesn't state how this will happen.  You are
specifically NOT supposed to make any assumptions about
how the pointers need to be converted.  If you use legitmate
operations to convert the pointers, this is all taken care
of for you.  Why are you concerning yourself.

Even in GCC, all you have to do is add more than one base
class to the derived class and you guarantee that at least
one of the DERIVED->BASE conversions will need to change
the internal potiner value.

You provide to delete the value that is of the right type.
Note that if you are deleting a derived object whose pointer
has been converted to the base class, the base class NEEDS
to have it's destructor declared virtual for JUST THIS TYPE
OF ISSUE.  It is undefined behavior to do otherwise.

Again, you ought not to be concerned about the literal values
of the pointers, if you obey the language rules for conversion
and delete, things are guaranteed to work.   Trying to make
assumptions on how things work otherwise is going to generate
code that may not work in all instances.



Tue, 02 Mar 2004 22:12:22 GMT  
 address of base class and derived class different


Fri, 19 Jun 1992 00:00:00 GMT  
 address of base class and derived class different
To paraphrase the ARM (annotated reference manual). Any
derived class which contains a destructor should be
derived from a base class with a virtual desctructor IF
the derived object is EVER cast to a base pointer.

The slicing is necessary and EVERY compiler does it. In
many cases the compiler will yield the SAME numeric value
for the two cases so the effect is transparent.

If you add a virtual destructor (or any virtual function
in this case) to your base class you will find this to be
true using VC.

If memory serves me, the GNU compilers place the vtable
pointer at the END of the base area of the first base
class, which would yield the effect you are seeing. I may
be wrong on the details of this last part as it has been
well over six months since I have had to worry about
memory layout using and GNU compiler.

Quote:
>-----Original Message-----
>Message unavailable



Wed, 03 Mar 2004 00:41:50 GMT  
 address of base class and derived class different


Fri, 19 Jun 1992 00:00:00 GMT  
 address of base class and derived class different

Quote:

>when a base class does not have a virtual member, but the
>derived class does, the MSVC++ compiler 'slices' the vptr
>table from the object, when referencing the derived object
>through a pointer to the base class object.

>Linux GNU does not do this: there the address/pointer is
>the same in both cases.

>What does the C++ standard say about this?

This is all unspecified.

Quote:
>And if it is
>implementation dependent, how do you find out what address
>to provide to delete()?

The only pointers you can delete which are not the same in value and
type as what new returned to you are pointers to base class subobjects
(of those new'd objects) which have virtual destructors. Note that
this freedom doesn't apply to the array form of new. Violations of
these rules result in undefined behavior.

Quote:
>Sample code:

>#include <iostream>
>using namespace std;

>class B
>{
>public:
>    int i;
>};

>class D: public B
>{
>public:
>    virtual ~D(){}
>};

>void doSomething( B * pb )
>{
>    cout << hex << pb << endl;
>}

>void main()
>{
>    D * pd = new D;
>    cout << hex << pd << endl;
>    doSomething( pd );
>}

>Results in my system in:

>00301E60
>00301E64

This is fine. Per what I described above, it would be undefined for
doSomething() to delete pb in your example, because B does not have a
virtual dtor, and pb points to the B part of a D.

--
Doug Harrison [VC++ MVP]
Eluent Software, LLC
http://www.eluent.com
Tools for Visual C++ and Windows



Wed, 03 Mar 2004 02:22:34 GMT  
 address of base class and derived class different


Quote:
> I'm a C++ newbie, but don't you need to provide a virtual
> destructor in your base class anyway so you can use
> delete with a base pointer?

    True... if he is going to delete with a base pointer.  From the context
of the example, it doesn't appear he is going to.

--
Truth,
James Curran
www.NJTheater.com     (Professional)
www.NovelTheory.com  (Personal)
www.BrandsForLess.Com (Day Job)



Fri, 05 Mar 2004 09:21:44 GMT  
 address of base class and derived class different
    Both MSVC & GNU are slicing the object.  It's just that MSVC is placing
the vtble at the beginning of the object, while GCC is putting it at the
end.

--
Truth,
James Curran
www.NJTheater.com     (Professional)
www.NovelTheory.com  (Personal)
www.BrandsForLess.Com (Day Job)


Quote:
> when a base class does not have a virtual member, but the
> derived class does, the MSVC++ compiler 'slices' the vptr
> table from the object, when referencing the derived object
> through a pointer to the base class object.

> Linux GNU does not do this: there the address/pointer is
> the same in both cases.



Fri, 05 Mar 2004 09:23:19 GMT  
 address of base class and derived class different

Quote:



> > I'm a C++ newbie, but don't you need to provide a virtual
> > destructor in your base class anyway so you can use
> > delete with a base pointer?

>     True... if he is going to delete with a base pointer.  From the context
> of the example, it doesn't appear he is going to.

I inferred it from the OP's question, "How do I know what address to pass to delete?"

I was first inclined to answer:

"Just pass the address you got from the "new" that created it."

Then I read the example where the derived class pointer was passed as a base
class pointer argument to a function.

From that, I thought the implication was:

"I was planning to use a delete statement in a similar base-class-argument function,
not shown in this sample code"

(which I figured was merely written to show the difference in addresses).

Rufus



Fri, 05 Mar 2004 21:18:51 GMT  
 
 [ 11 post ] 

 Relevant Pages 

1. How to Create CCtrlView derived class based on CTreeCtrl derived class

2. How to Create CCtrlView derived class based on CTreeCtrl derived class

3. CArray of different classes derived from common base

4. CArray of different classes derived from common base

5. How to Stop Derived Class from overriding base class method

6. creating an instance of a derived class from a base class

7. How to prevent derived classes from changing a base class function

8. Deriving from CControlBar - no base class in New Class dialog

9. CDialog derived class as a base class for another dialog box

10. serializing a class derived from an abstract base class

11. Serialize class derived from virtual base class

12. How do I cast base class to a derived class

 

 
Powered by phpBB® Forum Software