473,396 Members | 1,714 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Casting changes value of ptr

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
Jul 19 '05 #1
5 2257

"Vinodh Kumar" <th*************@yahoo.com> wrote in message
news:bg**********@news.mch.sbs.de...
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


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
Jul 19 '05 #2
John Harrison wrote:


It can happen, for instance if the derived class has virtual functions and
the base class does not.
But having a base class with no virtual destructor is asking for trouble.

#include <iostream>
using namespace std;

class A
{
int a;
};
Note no virtual destructor.

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

int main()
{
B* b = new B;
A* a = b;
cout << a << ' ' << b<< '\n';
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.
}

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++?


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

-Kevin

Jul 19 '05 #3
----- Original Message -----
From: "Kevin Goodsell" <us*********************@neverbox.com>
Newsgroups: comp.lang.c++
Sent: Thursday, July 31, 2003 12:22 PM
Subject: Re: Casting changes value of ptr

John Harrison wrote:


It can happen, for instance if the derived class has virtual functions and the base class does not.
But having a base class with no virtual destructor is asking for trouble.

#include <iostream>
using namespace std;

class A
{
int a;
};


Note no virtual destructor.

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

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


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.
}

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++?


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


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" <us*********************@neverbox.com> wrote in message
news:3f28bc98@shknews01... John Harrison wrote:


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


But having a base class with no virtual destructor is asking for trouble.

#include <iostream>
using namespace std;

class A
{
int a;
};


Note no virtual destructor.

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

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


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.
}

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++?


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

-Kevin

Jul 19 '05 #4

"tom_usenet" <to********@hotmail.com> wrote in message
news:3f***************@news.easynet.co.uk...
On Thu, 31 Jul 2003 12:46:35 +0530, "Vinodh Kumar"
<th*************@yahoo.com> wrote:
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 singleand multiple inheritance hierarchies, casting changes the value of the
pointer.I agree that the exact value of the point doesn't matter.


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.

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.


Jul 19 '05 #5
On Fri, 1 Aug 2003 12:06:37 +0530, "Vinodh Kumar"
<th*************@yahoo.com> wrote:
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)
Ok, there are certainly implementations that will use that layout.

Now, if the B object is created at the address 10, say, then the
object will run from address 10 though to 24.
?? You don't create a B object on the line below, but a C object.
C* c = new C;
A* a = C;

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

B* b = c;
Now the value of b will be different.
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.
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.


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
Jul 19 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Jacob Jensen | last post by:
This question has probably been asked a million time, but here it comes again. I want to learn the difference between the three type cast operators: static_cast, reinterpret_cast, dynamic_cast. A...
231
by: Brian Blais | last post by:
Hello, I saw on a couple of recent posts people saying that casting the return value of malloc is bad, like: d=(double *) malloc(50*sizeof(double)); why is this bad? I had always thought...
18
by: Visual Systems AB \(Martin Arvidsson\) | last post by:
Hi! I have created an enum list like this: enum myEnum : int { This = 2, That, NewVal = 10, LastItm
61
by: Ken Allen | last post by:
I am relatively new to .Net, but have been using VB and C/C++ for years. One of the drawbacks with VB6 and earlier was the difficulty in casting a 'record' to a different 'shape' so one could...
23
by: René Nordby | last post by:
Hi there, Is there anyone that knows how to do the following? I have a class A and a class B, that 100% inherits from class A (this means that I don't have other code in class B, than...
7
by: S. Lorétan | last post by:
Hi guys, Sorry for this stupid question, but I don't know why it isn't working. Here is my (example) code: namespace Test { class A { public string Label1; }
8
by: Neha | last post by:
Hi all Its seems bit silly que but pleasee explan me why this error is coming ? and what will be the solution if i want void * to be intilaize by struct * and this code is puerly in C. --...
5
by: brekehan | last post by:
I've always been a little sketchy on the differences between static, dynamic, and reinterpret casting. I am looking to clean up the following block by using C++ casting instead of the C style...
101
by: Tinkertim | last post by:
Hi, I have often wondered if casting the return value of malloc() (or friends) actually helps anything, recent threads here suggest that it does not .. so I hope to find out. For instance : ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.