const_cast confusion


 
Thread Tools Search this Thread
Top Forums Programming const_cast confusion
# 1  
Old 01-22-2011
const_cast confusion

See code below. It appears that i and j inhabit the same address yet hold different values. Can anyone shed light on this?

Code:
int main() {
  const int i= 3;
  int* j = const_cast<int*>(&i);
  *j = 5;
  cout << j << endl << &i << endl;
  cout << *j << endl << i;
}

# 2  
Old 01-22-2011
Interesting. What compiler are you using?

I understand that if you cast away the constness of an object that has been explicitly declared as const, and attempt to modify it, the results are undefined.
# 3  
Old 01-23-2011
I'm using GNU GCC compiler.

If an operation is specified as having an undefined result in the language standard, does the compiler documentation tell how they implemented it?
# 4  
Old 01-23-2011
"undefined result" means "don't do that".

what does your code actually print?
# 5  
Old 01-24-2011
Typically, in terms of conformance to a standard, undefined means the value or behavior may vary among implementations that conform to a standard. An application should not rely on the existence or validity of the value or behavior. Any application that relies on any particular value or behavior cannot be assured to be portable across conforming implementations.

Here is the example
Code:
#include <iostream>

using namespace std;

int main() {
  const int i= 3;
  int* j = const_cast<int*>(&i);
  cout << j << endl << &i << endl;
  *j = 5;
  cout << *j << endl << i << endl;
}

whose output is:
Code:
0x81ffb0
0x81ffb0
5
3

In general, const_cast is safe only if you're casting a variable that was originally non-const.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Confusion in hash

Hi folks, If a declare a direct hash , then the hash element works fine. my %test = ("test",1); print %test; print "\n"; Here in the above, the name of the hash is predeclared... Suppose now I need to create the hash elements dynamically in the for loop. $test="hash"; my... (1 Reply)
Discussion started by: scriptscript
1 Replies

2. Shell Programming and Scripting

confusion in use of exit 0

hi i am new to shell scripting. i was going thru the part option and arguments. on this section i fail to understand the use of exit 0 in below example . #!/bin/sh USAGE="Usage: $0 " case "$1" in -t) TARGS="-tvf $2" ;; -c) TARGS="-cvf $2.tar $2" ;; *) echo "$USAGE" exit 0 ;; esac... (13 Replies)
Discussion started by: scriptor
13 Replies

3. Shell Programming and Scripting

Confusion with PS

Hello All, I have a problem in counting number of process getting run with my current script name.. Here it is ps -ef | grep $0 | grep -v grep This display just one line with the PID, PPID and other details when i print it in the script. But when I want to count the numbers in my... (11 Replies)
Discussion started by: sathyaonnuix
11 Replies

4. Homework & Coursework Questions

Server Confusion

I don't even know where to start with this one. There is so much out there about different aspects of this. I am starting with a basic Ubuntu 11.04 install. Do I need to configure a DNS? I am a little confused about that. What do I need to do for a domain name? I have followed various tutorials,... (1 Reply)
Discussion started by: polyglot0727
1 Replies

5. Programming

Why this const variable is not changing even after applying const_cast?

Hi In the following code, why the variable 'i' still retains the value 10 instead of 11? Why it is not possible to alter this variable's value? int main() { const int i = 10; *(const_cast<int*>(&i)) = 11; cout << i << endl; // Ans: 10 } (6 Replies)
Discussion started by: royalibrahim
6 Replies

6. Programming

C fork Confusion :-?

Hi, I was trying to learn forking in C in UNIX. Somehow i still haven't been able to get the concept well. I mean, i do understand that fork creates an exact replica of the parent (other than the fact that parent gets the process id of the child and child gets 0 when fork is called). This is the... (2 Replies)
Discussion started by: ralpheno
2 Replies

7. UNIX for Dummies Questions & Answers

crontab confusion

I come across an entry in cron which is in such: 0 * * * * What is the first 0 indicating? 0 minute? meaning a script cron as such will run every minute? :confused: (2 Replies)
Discussion started by: user50210
2 Replies

8. UNIX for Dummies Questions & Answers

'tr' confusion

Good day, everyone! Could anybody explain me the following situation. If I'm running similar script: Var="anna.kurnikova" Var2="Anna Kurn" echo $Var | tr -t "$Var" "$Var2" Why the output is : anna KurniKova instead of Anna Kurnikova? :confused: Thank you in advance for any... (2 Replies)
Discussion started by: Nafanja
2 Replies

9. UNIX for Dummies Questions & Answers

unix confusion

:confused: some one please tell me where i can possibly find out what is unix 10.2 and the basic system functions of it is. I really need help! (1 Reply)
Discussion started by: tribb24
1 Replies
Login or Register to Ask a Question