473,395 Members | 1,577 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,395 software developers and data experts.

Problem consant_cast in C++

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
3 1145
svlsr2000
181 Expert 100+
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
Avi23
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
9,208 Expert Mod 8TB
#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

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

Similar topics

0
by: Bruce Davis | last post by:
I'm having a problem on windows (both 2000 and XP) with a multi-threaded tkinter gui application. The problem appears to be a deadlock condition when a child thread pops up a Pmw dialog window in...
11
by: Kostatus | last post by:
I have a virtual function in a base class, which is then overwritten by a function of the same name in a publically derived class. When I call the function using a pointer to the derived class...
0
by: Refky Wahib | last post by:
Hi I need Technical Support I finished a Great project using .Net and SQL Server and .Net Mobile Control My Business case is to implement this Program to accept about 1 Million concurrent...
9
by: Sudesh Sawant | last post by:
Hello, We have an application which communicates using remoting. There is a server which is a Windows Service. The server exposes an object which is a singleton. The client is a Web Application...
117
by: Peter Olcott | last post by:
www.halting-problem.com
17
by: Jon Slaughter | last post by:
I'm having a little trouble understanding what the slicing problem is. In B.S.'s C++ PL3rdEd he says "Becayse the Employee copy functions do not know anything about Managers, only the Employee...
28
by: Jon Davis | last post by:
If I have a class with a virtual method, and a child class that overrides the virtual method, and then I create an instance of the child class AS A base class... BaseClass bc = new ChildClass();...
6
by: Ammar | last post by:
Dear All, I'm facing a small problem. I have a portal web site, that contains articles, for each article, the end user can send a comment about the article. The problem is: I the comment length...
16
by: Dany | last post by:
Our web service was working fine until we installed .net Framework 1.1 service pack 1. Uninstalling SP1 is not an option because our largest customer says service packs marked as "critical" by...
2
by: Mike Collins | last post by:
I cannot get the correct drop down list value from a drop down I have on my web form. I get the initial value that was loaded in the list. It was asked by someone else what the autopostback was...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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.