Click here to Skip to main content
15,902,842 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: A funny thing happened to me. Who can explain? Pin
toxcct13-May-04 2:52
toxcct13-May-04 2:52 
GeneralRe: A funny thing happened to me. Who can explain? Pin
Antony M Kancidrowski13-May-04 2:53
Antony M Kancidrowski13-May-04 2:53 
GeneralRe: A funny thing happened to me. Who can explain? Pin
Cedric Moonen13-May-04 2:54
Cedric Moonen13-May-04 2:54 
GeneralRe: A funny thing happened to me. Who can explain? Pin
Antony M Kancidrowski13-May-04 2:50
Antony M Kancidrowski13-May-04 2:50 
GeneralRe: A funny thing happened to me. Who can explain? Pin
toxcct13-May-04 2:55
toxcct13-May-04 2:55 
GeneralRe: A funny thing happened to me. Who can explain? Pin
Antony M Kancidrowski13-May-04 3:22
Antony M Kancidrowski13-May-04 3:22 
AnswerRe: A funny thing happened to me. Who can explain? Pin
nguyenvhn13-May-04 2:58
nguyenvhn13-May-04 2:58 
AnswerRe: A funny thing happened to me. Who can explain? Pin
Christopher Lloyd13-May-04 3:02
Christopher Lloyd13-May-04 3:02 
Well it gives a==100 on my compiler! But i==10. I'd imagine that the problem was set to explain why i != a.
You're right that the compiler effectively replaces all references to a with the value 10, and that's why i == 10 (because the compiler sees this line as being i = 10) but the value a does have a physical address and using the two lines:
<br />
int* p=(int*)&a;<br />
*p=100;<br />

should change a.
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:

<br />
1844: const int a=10;<br />
0050D1BF   mov         dword ptr [a],0Ah<br />
1845: int* p=(int*)&a;<br />
0050D1C6   lea         eax,[a]<br />
0050D1C9   mov         dword ptr [p],eax<br />
1846: *p=100;<br />
0050D1CC   mov         ecx,dword ptr [p]<br />
0050D1CF   mov         dword ptr [ecx],64h<br />
1847: int i=a;<br />
0050D1D5   mov         dword ptr [i],0Ah<br />

Here it's clear that a will be changed to 100 in line 1846 and also that i 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 change a using a line like a = 20; it doesn't add some kind of 'lock' to a meaning it can never change.

Oh yeah, I've just spotted why a is 10 in your output. Because you output a which has been replaced at compile time with 10!!
GeneralRe: A funny thing happened to me. Who can explain? Pin
Cedric Moonen13-May-04 3:09
Cedric Moonen13-May-04 3:09 
GeneralRe: A funny thing happened to me. Who can explain? Pin
Antony M Kancidrowski13-May-04 3:53
Antony M Kancidrowski13-May-04 3:53 
GeneralRe: A funny thing happened to me. Who can explain? Pin
Nitron14-May-04 8:37
Nitron14-May-04 8:37 
AnswerRe: A funny thing happened to me. Who can explain? Pin
Antony M Kancidrowski13-May-04 3:19
Antony M Kancidrowski13-May-04 3:19 
AnswerRe: A funny thing happened to me. Who can explain? Pin
David Crow13-May-04 5:17
David Crow13-May-04 5:17 
GeneralRe: A funny thing happened to me. Who can explain? Pin
toxcct13-May-04 5:36
toxcct13-May-04 5:36 
GeneralRe: A funny thing happened to me. Who can explain? Pin
David Crow13-May-04 5:41
David Crow13-May-04 5:41 
GeneralRe: A funny thing happened to me. Who can explain? Pin
toxcct13-May-04 5:44
toxcct13-May-04 5:44 
GeneralRe: A funny thing happened to me. Who can explain? Pin
David Crow13-May-04 6:28
David Crow13-May-04 6:28 
GeneralRe: A funny thing happened to me. Who can explain? Pin
toxcct13-May-04 6:35
toxcct13-May-04 6:35 
GeneralRe: A funny thing happened to me. Who can explain? Pin
David Crow13-May-04 6:48
David Crow13-May-04 6:48 
AnswerRe: A funny thing happened to me. Who can explain? Pin
vividtang14-May-04 0:29
vividtang14-May-04 0:29 
GeneralRe: A funny thing happened to me. Who can explain? Pin
Anand Paranjpe14-May-04 0:37
Anand Paranjpe14-May-04 0:37 
AnswerRe: A funny thing happened to me. Who can explain? Pin
Anand Paranjpe14-May-04 0:34
Anand Paranjpe14-May-04 0:34 
GeneralSharing violation error when loading a DLL Pin
DaFrawg13-May-04 1:08
DaFrawg13-May-04 1:08 
GeneralRe: Sharing violation error when loading a DLL Pin
bikram singh13-May-04 5:28
bikram singh13-May-04 5:28 
GeneralRe: Sharing violation error when loading a DLL Pin
DaFrawg23-May-04 22:45
DaFrawg23-May-04 22:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.