PDA

View Full Version : [SOLVED] Undefined Const behaviour



c2tarun
March 16th, 2012, 03:02 PM
#include <stdio.h>

main(){
const int i=123;
const int *p;
p=&i;
int *np;
np = (int*)p;
*np = 98273987;
printf("%d\n",i);
}




According to my understanding the value of a const variable is constant and cannot be changed. But on executing the above program I am getting the output as 98273987 which is impossible, I should get an error. Can anyone please explain me this behavior.

muteXe
March 16th, 2012, 03:14 PM
what happens if you declare *np as const?

muteXe
March 16th, 2012, 03:16 PM
edit: I am not getting the same output as you report, i'm getting 123...

c2tarun
March 16th, 2012, 03:26 PM
Which compiler are you using? I am using gcc4.6.1

Zugzwang
March 16th, 2012, 03:36 PM
"const" means that the value *is supposed to be* constant, not that the compiler will actually enforce it to be constant. In the line " np = (int*)p;" you explicitly cast the "const" away, telling the compiler that you don't want it to care about constness in this case. Overwriting a const variable is a bad idea as optimizations by the compiler might or might not affect the value of the constant variable (I might be mistaken here, but it is in any case a bad idea).

muteXe
March 16th, 2012, 03:37 PM
visual studio 2010.
incidently, if you do my suggestion in post #2 the compiler does complain.

c2tarun
March 16th, 2012, 03:45 PM
"const" means that the value *is supposed to be* constant,
Sorry I am not able to understand the above line :( can you please explain a bit.



not that the compiler will actually enforce it to be constant. In the line " np = (int*)p;" you explicitly cast the "const" away, telling the compiler that you don't want it to care about constness in this case.What do you mean by casting the const away?


Overwriting a const variable is a bad idea as optimizations by the compiler might or might not affect the value of the constant variable (I might be mistaken here, but it is in any case a bad idea).
What do you mean by Overwriting a const variable?

codemaniac
March 16th, 2012, 03:48 PM
const variable is constant , as long as you are not manipulating its value via its base address .
see the code below .


#include <stdio.h>

main(){
const int i=123;

int *p;
p=&i;
*p=789;
printf("%d\n",i);
}

muteXe
March 16th, 2012, 03:49 PM
When you declare something as const, you are actually only making a suggestion to the compiler to do it.

On casting: http://www.cprogramming.com/tutorial/const_correctness.html

Arndt
March 16th, 2012, 04:01 PM
What do you mean by casting the const away?



(int *)p is a cast, and since it doesn't use the keyword 'const', you're telling the compiler that it is allowed to modify things pointed to by p. That is a lie, since you declared i const. In C you're allowed to lie to the compiler like this, but you are responsible for the consequences. A proper cast would be (const int *)p.

c2tarun
March 17th, 2012, 03:09 AM
(int *)p is a cast, and since it doesn't use the keyword 'const', you're telling the compiler that it is allowed to modify things pointed to by p. That is a lie, since you declared i const. In C you're allowed to lie to the compiler like this, but you are responsible for the consequences. A proper cast would be (const int *)p.

Thanks for the explanation guys :) I got it now.
I am marking this thread as solved.