LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-07-2003, 01:20 AM   #1
Xiangbuilder
Member
 
Registered: Apr 2003
Location: Shandong province China
Distribution: fedora core 1.0
Posts: 206

Rep: Reputation: 30
const_cast


I guess I can const_cast a const variable to nonconst, so I can change its value. But why the program below output *j=0xbffff2e4 instead of *j=3?
Code:
#include<iostream>
using namespace std;

int main ()
{
   const int i=0;
   cout<<"i="<<i<<endl;
   int* j=const_cast<int*>(&i); //preferred
   *j=3;
   cout<<"i="<<i<<endl;
   cout<<"*j="<<j<<endl;
   j=(int*)&i; //deprecated form
}
//: c03:const_cast.cpp
[root@localhost b_const]# g++ const_cast_a.cpp -o const_cast_a
[root@localhost b_const]# ./const_cast_a
i=0
i=0
*j=0xbffff2e4
[root@localhost b_const]#
thank you.
 
Old 10-07-2003, 05:42 AM   #2
dimm_coder
LQ Newbie
 
Registered: Oct 2003
Location: Minsk, Belarus
Distribution: Mandrake, FreeBSD
Posts: 28

Rep: Reputation: 15
Well, this problem caused by compiler optimization.
While compiling, it puts the value of i to all places of its output (it
thinks that it is a constant, like it is declared, so for optimization
purposes it makes so).
But try the next sample:
const_cast<int&>(i) = 1;
cout << const_cast<int&>(i) << endl;

So U'll get the value = 1 on output.

Nevertheless there is a more important possible problem
with const_cast using.
If const data is placed by compiler in section where writing is denied (f.e. in code section)
and U can try to use such type of code:

struct x { int a; };

const x a = {0};

int main()
{
const_cast<x&>(a).a = 1;
return 0;
}

Bang!!! Segmenattion fault!

So, as result, U need to take advanced care while using const_cast or
donot use it simply.
 
Old 10-07-2003, 08:15 AM   #3
Xiangbuilder
Member
 
Registered: Apr 2003
Location: Shandong province China
Distribution: fedora core 1.0
Posts: 206

Original Poster
Rep: Reputation: 30
Thank you.
 
Old 10-07-2003, 09:07 AM   #4
abrb220
Member
 
Registered: Jul 2003
Location: Roodepoort South Africa
Distribution: Redhat / Suse
Posts: 39

Rep: Reputation: 15
Er...
Looks to me like a typo...the line

cout<<"*j="<<j<<endl;

should be

cout<<"*j="<<*j<<endl;

You were actually displaying the pointer and not the contents.
 
Old 10-07-2003, 09:26 AM   #5
dimm_coder
LQ Newbie
 
Registered: Oct 2003
Location: Minsk, Belarus
Distribution: Mandrake, FreeBSD
Posts: 28

Rep: Reputation: 15
Quote:
Originally posted by abrb220
Er...
Looks to me like a typo...the line

cout<<"*j="<<j<<endl;

should be

cout<<"*j="<<*j<<endl;

You were actually displaying the pointer and not the contents.
Yep, but this error doesn't give any big changes to this problem. The main problem doesn't depends on this.
 
Old 10-07-2003, 09:36 AM   #6
Xiangbuilder
Member
 
Registered: Apr 2003
Location: Shandong province China
Distribution: fedora core 1.0
Posts: 206

Original Poster
Rep: Reputation: 30
sorry for my simple mistake.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:17 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration