By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,949 Members | 946 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,949 IT Pros & Developers. It's quick & easy.

Problem consant_cast in C++

P: 2
Hi
I am new to this community . I have a sily doubt. can u tell why in this bellow program both &i and p have the same address but when i am derefrencing both they are giving differnt value. then can u tell when both these value are stored. plz reply

#include<iostream>
using namespace std;
int main()
{
const int i=10;
int *p=const_cast<int *>(&i);
*p=200;
cout<<p<<" "<<*p<<endl;
cout<<&i<<" "<<i<<endl;
return 0;
}
Jun 20 '07 #1
Share this Question
Share on Google+
3 Replies


Expert 100+
P: 181
Hi
I am new to this community . I have a sily doubt. can u tell why in this bellow program both &i and p have the same address but when i am derefrencing both they are giving differnt value. then can u tell when both these value are stored. plz reply

#include<iostream>
using namespace std;
int main()
{
const int i=10;
int *p=const_cast<int *>(&i);
*p=200;
cout<<p<<" "<<*p<<endl;
cout<<&i<<" "<<i<<endl;
return 0;
}
y have you posted twice.
http://www.thescripts.com/forum/thread665277.html
Jun 20 '07 #2

P: 7
Hi All

m new to this community so i was just looking at few back dated posts.I also hav one doubt in this regard. i gone through the link u mention in ur rply but my doubt is: if compiler is creating the copy of the const variable for pointer , then it should carry the address of that copy not the address of varibale where it is originally stored.means according to me the address of const variable and the the address stored in pointer should be different.

Could you pls help me in understanding this ?

Thnx
Jun 20 '07 #3

weaknessforcats
Expert Mod 5K+
P: 9,197
#include<iostream>
using namespace std;
int main()
{
const int i=10;
int *p=const_cast<int *>(&i);
*p=200;
cout<<p<<" "<<*p<<endl;
cout<<&i<<" "<<i<<endl;
return 0;
}
This code is a prime example of why casting is bad news in C++.

In this case, i is a constant with a value of 10. That can never change.

When you cast, you are asking the compiler to take the address of the constant and make it a non-constant. The compiler won't do that because it knows you will use the cast to change the constant. So, it makes a copy of i and hides it. Now whenever you use *p you see the int at the original address but when you use i, the compiler trots out its copy and lets you use that.

You have noticed that the address of i and the address in the pointer are the same. This is courtesy of the compiler. It tries to make you think both values are at the same location.

This is so ugly because the part of your program using i uses a different value than the part that uses *p.

The cast has destroyed the type safety of your program.

If you try this in C you get a different result. In C, you get to change the constant because C is really crude in this area.
Jun 20 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.