A problem about the "const" of C++
#include "stdafx.h"
#include <iostream.h> int main(int argc, char* argv[]) { const int i=10; int *pi = (int *) &i; const int *pii = &i; int j=99; int *pj=&j; *pj=10000; *pi=33; cout << i << endl; cout << *pi << endl; cout << pi << endl; cout << pii << endl; cout << *pii << endl; cout << j << endl; return 0; } the result is following: 10 33 0x0012FF7C 0x0012FF7C 33 10000 why can not i change "i" through the pointer pi but can change "j" through the pointer pj; the value of "pi" and "pii" is the address of "i" |
Why would you change the value of a 'const'?
I'd say you were asking for trouble and you got it all right. Still, you can try 'const volatile int i = 10;' |
Quote:
besides ,i think the reason is not important i exactly get the adress of "i",and assin it to "pi" and i change "*pi", we can see that the content of the adress of "i" is changed but when i look up it through "i", it can not show the change of the content. why why why why why why why why why why why why why ? |
Cos cos cos cos cos cos cos cos the compiler thought it might safely use literal value 10 for i, for it is a 'const'
Note: you would get a bit more clear messages if you used printf: Code:
printf ("i is at %p, value=%d\n", (void *)&i, i); |
Quote:
You are right maybe ,the keypoint is the literal value the content of the address 0x0012FF7c has been changed but the compiler tie "i" to "10"; |
looks like an error for me:
int *pi = (int *) &i; if i was a register or used inline, there is no address of. |
Quote:
Quote:
|
Quote:
I have tried what you said and defined a global variable ,the program was terminated but i don't understand what you say about "inline" what's that mean? |
Quote:
|
Quote:
Code:
$ cat a.c |
Quote:
|
If you take its address, the compiler will assign a memory location on the stack, but it still has the right to use the literal value of 10 whenever you use 'i'.
|
Quote:
Code:
$ cat a.c && gcc -S -O2 -fomit-frame-pointer a.c && cat a.s |
Quote:
I'm trying to say that in the very nicest possible way, and with the purpose of being helpful. (This is not "RTFM.™") Because, what you're doing right now is banging your head against things that you barely (if at all) understand, when the ability to fully understand them will only take a little more of the time that you are wasting right now. Quit stumbling around in the dark: turn on the lights, get a map, and stop and read it. You have a well-defined goal to reach ("understanding"), and what you're doing right now is a hopelessly-inefficient non-method of getting there. |
Quote:
Either it should not create a memory location for the constant - meaning that attempting to set a pointer to the address of the constant gives an error message or It creates a memory location to store the value of the constant but any attempt to change the contents of that memory location would result in an error message. Of course, in normal programming practice, once you have decided that something is to be a constant, there should be no reason to refer to its address nor to change its value so this unusual situation would not crop up. |
All times are GMT -5. The time now is 12:07 AM. |