A funny thing happened to me. Who can explain?
-
Well it gives
a==100
on my compiler! Buti==10
. I'd imagine that the problem was set to explain whyi != a
. You're right that the compiler effectively replaces all references toa
with the value 10, and that's whyi == 10
(because the compiler sees this line as beingi = 10
) but the valuea
does have a physical address and using the two lines:int* p=(int*)&a; *p=100;
should changea
. If I was you I'd debug the code and then look at the disassembly, there everything should become clear. In my compiler it gives this:1844: const int a=10; 0050D1BF mov dword ptr [a],0Ah 1845: int* p=(int*)&a; 0050D1C6 lea eax,[a] 0050D1C9 mov dword ptr [p],eax 1846: *p=100; 0050D1CC mov ecx,dword ptr [p] 0050D1CF mov dword ptr [ecx],64h 1847: int i=a; 0050D1D5 mov dword ptr [i],0Ah
Here it's clear thata
will be changed to 100 in line 1846 and also thati
will be set to 10 in line 1847. By the way, const isn't magic, it just tells the compiler that you shouldn't be able to changea
using a line likea = 20;
it doesn't add some kind of 'lock' toa
meaning it can never change. Oh yeah, I've just spotted whya
is 10 in your output. Because you outputa
which has been replaced at compile time with 10!!Yea it seems that the compiler has optimised itself here since a was declared const! :) Ant.
-
When doing my homework, I had this code:
.... int main(){ const int a=10; int* p=(int*)&a; *p=100; int i=a; cout << "p=" << p << endl; cout << "&a=" << &a << endl; cout << "*p=" << *p << endl; cout << "a=" << a << endl; cout << "i=" << i << endl; return 0; }
Here is the output: p=0012FF7C &a=0012FF7C *p=100 a=10 i=10 I guessed thata
had to be 100, but it's 10. It seem that compiler has replaced all instance of variablea
to 10 at compile time, event ifa
has been allocated a storage. So the trick to change a constant by casting its pointer is not effect. Is that right? Any comment is welcome. Thanks.nguyenvhn wrote: const int a=10; This simply means that you cannot modify memory address 0x0012FF7C through variable
a
. The address is not read-only, the variable is. make sense?
"The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)
-
nguyenvhn wrote: const int a=10; This simply means that you cannot modify memory address 0x0012FF7C through variable
a
. The address is not read-only, the variable is. make sense?
"The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)
-
teu teu teu, read the other posts before saying so, especially "Christopher Lloyd"'s one :(
TOXCCT >>> GEII power
In which case he states that "it just tells the compiler that you shouldn't be able to change a using a line like a = 20" In my post, I stated that "you cannot modify memory address 0x0012FF7C through variable a" What's your point?
"The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)
-
In which case he states that "it just tells the compiler that you shouldn't be able to change a using a line like a = 20" In my post, I stated that "you cannot modify memory address 0x0012FF7C through variable a" What's your point?
"The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)
-
yes you can
const
is only a specification for the compiler!!! if you had read all the posts here, you would have seen that we already said that also... and i even that i was thinking like you ! :mad:
TOXCCT >>> GEII power
Then how would you propose changing the value through variable
a
?
"The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)
-
Then how would you propose changing the value through variable
a
?
"The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)
by a pointer to its address.
int* p = &a;
. this works, but badly, the compiler replaced each occurence ofa
by its value (10 here). that is why, even if the value contained at the adress of a is changed, the compiler used eacha
uses as a#define
.int d = a;
will affect 10 to d, even if the line before was*p = 100;
. Do we now understand better ?
TOXCCT >>> GEII power
-
by a pointer to its address.
int* p = &a;
. this works, but badly, the compiler replaced each occurence ofa
by its value (10 here). that is why, even if the value contained at the adress of a is changed, the compiler used eacha
uses as a#define
.int d = a;
will affect 10 to d, even if the line before was*p = 100;
. Do we now understand better ?
TOXCCT >>> GEII power
int *p = &a;
*p = 100;This does not change the value through variable
a
. The value is changed through variablep
. Do you still find my original "...you cannot modify memory address 0x0012FF7C through variable a. The address is not read-only, the variable is" statement to be false. I *think* we are saying the same thing, so I'm not quite sure why this thread is six posts longer than necessary.
"The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)
-
When doing my homework, I had this code:
.... int main(){ const int a=10; int* p=(int*)&a; *p=100; int i=a; cout << "p=" << p << endl; cout << "&a=" << &a << endl; cout << "*p=" << *p << endl; cout << "a=" << a << endl; cout << "i=" << i << endl; return 0; }
Here is the output: p=0012FF7C &a=0012FF7C *p=100 a=10 i=10 I guessed thata
had to be 100, but it's 10. It seem that compiler has replaced all instance of variablea
to 10 at compile time, event ifa
has been allocated a storage. So the trick to change a constant by casting its pointer is not effect. Is that right? Any comment is welcome. Thanks.#include using namespace std; int main(){ int a=10; int* p=(int*)&a; *p=100; int i=a; cout << "p=" << p << endl; cout << "&a=" << &a << endl; cout << "*p=" << *p << endl; cout << "a=" << a << endl; cout << "i=" << i << endl; return 0; } the running result p=0012FF7C &a=0012FF7C *p=100 a=100 i=100
-
When doing my homework, I had this code:
.... int main(){ const int a=10; int* p=(int*)&a; *p=100; int i=a; cout << "p=" << p << endl; cout << "&a=" << &a << endl; cout << "*p=" << *p << endl; cout << "a=" << a << endl; cout << "i=" << i << endl; return 0; }
Here is the output: p=0012FF7C &a=0012FF7C *p=100 a=10 i=10 I guessed thata
had to be 100, but it's 10. It seem that compiler has replaced all instance of variablea
to 10 at compile time, event ifa
has been allocated a storage. So the trick to change a constant by casting its pointer is not effect. Is that right? Any comment is welcome. Thanks.this is because of compiler optimization. Just have a look at assembly code. const int a=10; 0041B1CE mov dword ptr [a],0Ah int* p=(int*)&a; 0041B1D5 lea eax,[a] 0041B1D8 mov dword ptr [p],eax *p=100; 0041B1DB mov eax,dword ptr [p] 0041B1DE mov dword ptr [eax],64h int i=a; 0041B1E4 mov dword ptr [i],0Ah Also while printing on console... cout << "a=" << a << endl; 0041B291 push offset std::endl (4194BAh) 0041B296 push 0Ah 0041B298 push offset string "a=" (44E0C8h) 0041B29D push offset std::cout (457668h) 0041B2A2 call std::operator<< > (419A96h) 0041B2A7 add esp,8 0041B2AA mov ecx,eax 0041B2AC call std::basic_ostream >::operator<< (4195DCh) 0041B2B1 mov ecx,eax 0041B2B3 call std::basic_ostream >::operator<< (419AC3h) You will not experience this behavour with all the compilers. Thanks, Anand The chosen One :)
-
#include using namespace std; int main(){ int a=10; int* p=(int*)&a; *p=100; int i=a; cout << "p=" << p << endl; cout << "&a=" << &a << endl; cout << "*p=" << *p << endl; cout << "a=" << a << endl; cout << "i=" << i << endl; return 0; } the running result p=0012FF7C &a=0012FF7C *p=100 a=100 i=100
why are you tampering code by removing const while declaring variable a. it should be const int a=10; The chosen One :)
-
Well it gives
a==100
on my compiler! Buti==10
. I'd imagine that the problem was set to explain whyi != a
. You're right that the compiler effectively replaces all references toa
with the value 10, and that's whyi == 10
(because the compiler sees this line as beingi = 10
) but the valuea
does have a physical address and using the two lines:int* p=(int*)&a; *p=100;
should changea
. If I was you I'd debug the code and then look at the disassembly, there everything should become clear. In my compiler it gives this:1844: const int a=10; 0050D1BF mov dword ptr [a],0Ah 1845: int* p=(int*)&a; 0050D1C6 lea eax,[a] 0050D1C9 mov dword ptr [p],eax 1846: *p=100; 0050D1CC mov ecx,dword ptr [p] 0050D1CF mov dword ptr [ecx],64h 1847: int i=a; 0050D1D5 mov dword ptr [i],0Ah
Here it's clear thata
will be changed to 100 in line 1846 and also thati
will be set to 10 in line 1847. By the way, const isn't magic, it just tells the compiler that you shouldn't be able to changea
using a line likea = 20;
it doesn't add some kind of 'lock' toa
meaning it can never change. Oh yeah, I've just spotted whya
is 10 in your output. Because you outputa
which has been replaced at compile time with 10!!