Connecting Tech Pros Worldwide Forums | Help | Site Map

Casting changes value of ptr

Vinodh Kumar
Guest
 
Posts: n/a
#1: Jul 19 '05
I see that casting changes the value of a pointer in case of multiple
inheritance.In single inheritance also it is the same know?Isn't it?

Vinodh Kumar P



John Harrison
Guest
 
Posts: n/a
#2: Jul 19 '05

re: Casting changes value of ptr



"Vinodh Kumar" <thecomdeveloper@yahoo.com> wrote in message
news:bga8us$j8q$1@news.mch.sbs.de...[color=blue]
> I see that casting changes the value of a pointer in case of multiple
> inheritance.In single inheritance also it is the same know?Isn't it?
>
> Vinodh Kumar P
>[/color]

It can happen, for instance if the derived class has virtual functions and
the base class does not.

#include <iostream>
using namespace std;

class A
{
int a;
};

class B : public A
{
virtual ~B() {}
int b;
};

int main()
{
B* b = new B;
A* a = b;
cout << a << ' ' << b<< '\n';
}

Basically this is an area where you shouldn't mind if a cast or conversion
changes the pointer value. Do you think its a problem, or are you just
trying to learn more about C++?

john


Kevin Goodsell
Guest
 
Posts: n/a
#3: Jul 19 '05

re: Casting changes value of ptr


John Harrison wrote:
[color=blue]
>
>
> It can happen, for instance if the derived class has virtual functions and
> the base class does not.[/color]

But having a base class with no virtual destructor is asking for trouble.
[color=blue]
>
> #include <iostream>
> using namespace std;
>
> class A
> {
> int a;
> };[/color]

Note no virtual destructor.
[color=blue]
>
> class B : public A
> {
> virtual ~B() {}
> int b;
> };
>
> int main()
> {
> B* b = new B;
> A* a = b;
> cout << a << ' ' << b<< '\n';[/color]

Assuming you want to prevent memory leaks, you'd obviously need to free
the allocated object. This:

delete a;

Creates undefined behavior. In this particular case it's easy to work
around. You just do this instead:

delete b;

But in general you may have a pointer of type A* which may point to some
derived class, which is not known. The only safe way to delete it (in
the absence of a virtual destructor) is to first cast it to the exact
correct pointer type. This can obviously be a major hassle.
[color=blue]
> }
>
> Basically this is an area where you shouldn't mind if a cast or conversion
> changes the pointer value. Do you think its a problem, or are you just
> trying to learn more about C++?
>[/color]

I agree - usually, the exact value of a pointer should not matter.

-Kevin

Vinodh Kumar
Guest
 
Posts: n/a
#4: Jul 19 '05

re: Casting changes value of ptr


----- Original Message -----
From: "Kevin Goodsell" <usenet1.spamfree.fusion@neverbox.com>
Newsgroups: comp.lang.c++
Sent: Thursday, July 31, 2003 12:22 PM
Subject: Re: Casting changes value of ptr

[color=blue]
> John Harrison wrote:
>[color=green]
> >
> >
> > It can happen, for instance if the derived class has virtual functions[/color][/color]
and[color=blue][color=green]
> > the base class does not.[/color]
>
> But having a base class with no virtual destructor is asking for trouble.
>[color=green]
> >
> > #include <iostream>
> > using namespace std;
> >
> > class A
> > {
> > int a;
> > };[/color]
>
> Note no virtual destructor.
>[color=green]
> >
> > class B : public A
> > {
> > virtual ~B() {}
> > int b;
> > };
> >
> > int main()
> > {
> > B* b = new B;
> > A* a = b;
> > cout << a << ' ' << b<< '\n';[/color]
>
> Assuming you want to prevent memory leaks, you'd obviously need to free
> the allocated object. This:
>
> delete a;
>
> Creates undefined behavior. In this particular case it's easy to work
> around. You just do this instead:
>
> delete b;
>
> But in general you may have a pointer of type A* which may point to some
> derived class, which is not known. The only safe way to delete it (in
> the absence of a virtual destructor) is to first cast it to the exact
> correct pointer type. This can obviously be a major hassle.
>[color=green]
> > }
> >
> > Basically this is an area where you shouldn't mind if a cast or[/color][/color]
conversion[color=blue][color=green]
> > changes the pointer value. Do you think its a problem, or are you just
> > trying to learn more about C++?
> >[/color]
>
> I agree - usually, the exact value of a pointer should not matter.[/color]

But the type of the object that is being pointed to difers in case of a
casting in multiple inheritance.Thats the same case when we cast between
objects of single inheritance hiearchies.So I conclude that both in single
and multiple inheritance hierarchies, casting changes the value of the
pointer.I agree that the exact value of the point doesn't matter.

"Kevin Goodsell" <usenet1.spamfree.fusion@neverbox.com> wrote in message
news:3f28bc98@shknews01...[color=blue]
> John Harrison wrote:
>[color=green]
> >
> >
> > It can happen, for instance if the derived class has virtual functions[/color][/color]
and[color=blue][color=green]
> > the base class does not.[/color]
>
> But having a base class with no virtual destructor is asking for trouble.
>[color=green]
> >
> > #include <iostream>
> > using namespace std;
> >
> > class A
> > {
> > int a;
> > };[/color]
>
> Note no virtual destructor.
>[color=green]
> >
> > class B : public A
> > {
> > virtual ~B() {}
> > int b;
> > };
> >
> > int main()
> > {
> > B* b = new B;
> > A* a = b;
> > cout << a << ' ' << b<< '\n';[/color]
>
> Assuming you want to prevent memory leaks, you'd obviously need to free
> the allocated object. This:
>
> delete a;
>
> Creates undefined behavior. In this particular case it's easy to work
> around. You just do this instead:
>
> delete b;
>
> But in general you may have a pointer of type A* which may point to some
> derived class, which is not known. The only safe way to delete it (in
> the absence of a virtual destructor) is to first cast it to the exact
> correct pointer type. This can obviously be a major hassle.
>[color=green]
> > }
> >
> > Basically this is an area where you shouldn't mind if a cast or[/color][/color]
conversion[color=blue][color=green]
> > changes the pointer value. Do you think its a problem, or are you just
> > trying to learn more about C++?
> >[/color]
>
> I agree - usually, the exact value of a pointer should not matter.
>
> -Kevin
>[/color]


Vinodh Kumar
Guest
 
Posts: n/a
#5: Jul 19 '05

re: Casting changes value of ptr



"tom_usenet" <tom_usenet@hotmail.com> wrote in message
news:3f29021f.55382968@news.easynet.co.uk...[color=blue]
> On Thu, 31 Jul 2003 12:46:35 +0530, "Vinodh Kumar"
> <thecomdeveloper@yahoo.com> wrote:
>[color=green]
> >But the type of the object that is being pointed to difers in case of a
> >casting in multiple inheritance.Thats the same case when we cast between
> >objects of single inheritance hiearchies.So I conclude that both in[/color][/color]
single[color=blue][color=green]
> >and multiple inheritance hierarchies, casting changes the value of the
> >pointer.I agree that the exact value of the point doesn't matter.[/color]
>
> In single inheritence, in common implementations the cast shouldn't
> change the value of the pointer. Imagine:
>
> class A
> {
> int i;
> virtual ~A();
> };
>
> class B: public A
> {
> int j;
> };
>
> B* b = new B;
> A* a = b;
>
>
> Now, the layout of a B might look like this:
>
> vtable ptr (4 bytes)
> A part: int i; (4 bytes)
> B part: int j; (4 bytes)
>
> Now, if the B object is created at the address 10, say, then the
> object will run from address 10 though to 22.
>
> The value of b above will be 10.
> The value of a above will also be 10!
>
> IOW, the value of the pointer will not change in single inheritence
> cases.
>[/color]
class A
{
int i;
virtual ~A();
};

class B: public A
{
int j;
};

class C: public B
{
int k;
}

Now, the layout of a C might look like this:

vtable ptr (4 bytes)
A part: int i; (4 bytes)
B part: int j; (4 bytes)
C part: int k;(4 bytes)

Now, if the B object is created at the address 10, say, then the
object will run from address 10 though to 24.

C* c = new C;
A* a = C;

The value of c above will be 10.
The value of a above will also be 10.
Fine.

B* b = c;
Now the value of b will be different.
So in single inheritence casting changes the value of the pointer if we are
not casting to top most base class or bottom most derived class.








tom_usenet
Guest
 
Posts: n/a
#6: Jul 19 '05

re: Casting changes value of ptr


On Fri, 1 Aug 2003 12:06:37 +0530, "Vinodh Kumar"
<thecomdeveloper@yahoo.com> wrote:
[color=blue]
> class A
> {
> int i;
> virtual ~A();
> };
>
> class B: public A
> {
> int j;
> };
>
>class C: public B
>{
> int k;
>}
>
> Now, the layout of a C might look like this:
>
> vtable ptr (4 bytes)
> A part: int i; (4 bytes)
> B part: int j; (4 bytes)
>C part: int k;(4 bytes)[/color]

Ok, there are certainly implementations that will use that layout.
[color=blue]
>
> Now, if the B object is created at the address 10, say, then the
> object will run from address 10 though to 24.[/color]

?? You don't create a B object on the line below, but a C object.
[color=blue]
>C* c = new C;
> A* a = C;
>
> The value of c above will be 10.[/color]

Ok.
[color=blue]
> The value of a above will also be 10.
>Fine.[/color]

Ok.
[color=blue]
>
>B* b = c;
>Now the value of b will be different.[/color]

Why? It will still be 10 on normal implementations (I don't know of an
implementation where it will change, and there may well not be one,
although the standard allows it). The B object doesn't care whether
it's a subobject of a C object, a D object or whether it isn't a sub
object at all, but the whole object.
[color=blue]
>So in single inheritence casting changes the value of the pointer if we are
>not casting to top most base class or bottom most derived class.[/color]

Why do you think that the value will change in the above case? It
wouldn't make sense for it to change. Try this on your favourite
compilers:

class A
{
public:
int i;
virtual ~A(){}
};

class B: public A
{
public:
int j;
};

class C: public B
{
public:
int k;
};

#include <iostream>

int main()
{
C* c = new C;
B* b = c;
A* a = c;

std::cout << (void*)a << '\n';
std::cout << (void*)b << '\n';
std::cout << (void*)c << '\n';
}

Tom
Closed Thread