A funny thing happened to me. Who can explain?
-
toxcct wrote: where did you see it was 100 ?? Here:
int* p=(int*)&a; *p=100;
Because p (who is a pointer) receives the adress of the variable a and then you put 100 in the content pointed by p (so, at the same adress than a)... So you replace the value of a. But I don't understand why it prints 10 and not 100. The const keyword has something to do with it but I don't know what.... :confused: -
toxcct wrote: where did you see it was 100 ?? Here:
int* p=(int*)&a; *p=100;
Because p (who is a pointer) receives the adress of the variable a and then you put 100 in the content pointed by p (so, at the same adress than a)... So you replace the value of a. But I don't understand why it prints 10 and not 100. The const keyword has something to do with it but I don't know what.... :confused:if you do this: int a = 10; => a is the value AND &a is the pointer int* p = 100; => *p is the value AND p is the pointer thus if I see correctly: int* p=(int*)&a; means that *p = 10 (* and & are opposites) and p and &a are still different. (you should do p = &a; if you would like to copy pointers) Then again I could be mistaken (you always do working with pointers)
-
toxcct wrote: where did you see it was 100 ?? Here:
int* p=(int*)&a; *p=100;
Because p (who is a pointer) receives the adress of the variable a and then you put 100 in the content pointed by p (so, at the same adress than a)... So you replace the value of a. But I don't understand why it prints 10 and not 100. The const keyword has something to do with it but I don't know what.... :confused:and if you output *(int*)&a rather than a? ;P Ant.
-
if you do this: int a = 10; => a is the value AND &a is the pointer int* p = 100; => *p is the value AND p is the pointer thus if I see correctly: int* p=(int*)&a; means that *p = 10 (* and & are opposites) and p and &a are still different. (you should do p = &a; if you would like to copy pointers) Then again I could be mistaken (you always do working with pointers)
excuse me but
int* p=(int*)&a;
is as much correct as yourp = &a;
secondly, you say int a = 10; a is the value AND &a is the pointer. a say NO. &a is the ADDRESS !! and to finish, even if you're right, you don't answer the post to typed...
TOXCCT >>> GEII power
-
if you do this: int a = 10; => a is the value AND &a is the pointer int* p = 100; => *p is the value AND p is the pointer thus if I see correctly: int* p=(int*)&a; means that *p = 10 (* and & are opposites) and p and &a are still different. (you should do p = &a; if you would like to copy pointers) Then again I could be mistaken (you always do working with pointers)
If you try to do p = &a; in this example the compiler will complain that it can not convert from const int* to int* ! ;) Ant.
-
if you do this: int a = 10; => a is the value AND &a is the pointer int* p = 100; => *p is the value AND p is the pointer thus if I see correctly: int* p=(int*)&a; means that *p = 10 (* and & are opposites) and p and &a are still different. (you should do p = &a; if you would like to copy pointers) Then again I could be mistaken (you always do working with pointers)
V. wrote: int* p = 100; => *p is the value AND p is the pointer Yes but he is assigning the adress of a in p just before:
int* p=(int*)&a;
So p and &a are the same (in this case, the adress of &a). -
and if you output *(int*)&a rather than a? ;P Ant.
-
what do you think "const" mean ? constipated ? for me (and for C/C++ also), it means "constant", that is, you cannot modify it ! that's all....
TOXCCT >>> GEII power
Yes of course I know this (I'm not completely stupid ;P) but if you try to change the contents of it, you will have a compile error. Or here, you change it by another way (using the adress of the variable). And there is no errors BUT the adress p and the adress &a are the same but their contents are different so there is something strange (two pointers that are pointing at exactly the same adress but the values they hold are different X| )!! It would be normal if *p still contain 10 at the end (that menas the content of a was unchanged)
-
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.To all: const int a=10;//Declare an int constant and init 10 int* p=(int*)&a; //Declare an int pointer p and point to address of a. *p=100;//Change where p pointed to 100 int i=a;//Declare an int variable i and init i to value of a. cout << "p=" << p << endl;//Print p value (address) cout << "&a=" << &a << endl;//Print address of a cout << "*p=" << *p << endl;//Print value at where p pointed to cout << "a=" << a << endl;//Print value of a cout << "i=" << i << endl;//Print value of i And the result: p=0012FF7C //Point to the same address of a &a=0012FF7C //Point to the same address of p *p=100 //But one value is 100 a=10 //And here the other value is 10 i=10 //And here the value is 10 too. p and a have the same memory address. But it's value is difference. Why that? PS: if I write: int* p=&a, compiler will compain as an error. So I have used a trick by casting int* p=(int*)&a. In this case, compiler ignore.
-
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.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!! -
what do you think "const" mean ? constipated ? for me (and for C/C++ also), it means "constant", that is, you cannot modify it ! that's all....
TOXCCT >>> GEII power
-
Yes of course I know this (I'm not completely stupid ;P) but if you try to change the contents of it, you will have a compile error. Or here, you change it by another way (using the adress of the variable). And there is no errors BUT the adress p and the adress &a are the same but their contents are different so there is something strange (two pointers that are pointing at exactly the same adress but the values they hold are different X| )!! It would be normal if *p still contain 10 at the end (that menas the content of a was unchanged)
in fact, the compiler don't say anything because
const
is an identifier for the compiler, and as we don't explicitely write into variable a, there is no compile error. but a is still const (certainly not even in the stack...) so *p cannot access the value correctly to alter it.
TOXCCT >>> GEII power
-
what do you think "const" mean ? constipated ? for me (and for C/C++ also), it means "constant", that is, you cannot modify it ! that's all....
TOXCCT >>> GEII power
-
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!!Finally it's clear :-D !! Yes ok, I didn't know that the compiler really replace all occurences of a by it's value (so acting like a #define in fact). What did you win ;P ??
-
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.OK seriously, You should not cast away the const-ness of objects that were originally defined as being const and on which non-const operations are being executed. Doing this, results in undefined behaviour. http://www.uwyn.com/resources/uwyn_cpp_coding_standard/x629.html[^] Ant.
-
where do you see a difference between
*(int*)&a
anda
you, except in confusing the reader ?? :~
TOXCCT >>> GEII power
Try it in VC 6 you will be amazed, anyway see my other post WRT casting away const-ness. Ant.
-
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)