新建一个控制台项目,在主cpp中输入以下内容:#include "stdafx.h"
#include <iostream>
using namespace std; //注意,如果不是vc7,头文件的引用可能与此不同int main(int argc)
{
const int i = 108;
int *p = (int *)&i; *p = 109; cout << "address of i:" << &i << endl;  //i的地址
cout << "address of p:" << p << endl;   //p所指向的地址
cout << "i  = " << i << endl;           //i的内容
cout << "*p = " << *p << endl;          //p指向的内容 system("pause");
return 0;
}结果如下:
address of i:0013FED4
address of p:0013FED4
i  = 108
*p = 109奇怪的是,明明i和p的地址相同,但他们的值却不同??

解决方案 »

  1.   

    #include "stdafx.h"
    int _tmain(int argc, _TCHAR* argv[])
    {
    00413350  push        ebp  
    00413351  mov         ebp,esp 
    00413353  sub         esp,0D8h 
    00413359  push        ebx  
    0041335A  push        esi  
    0041335B  push        edi  
    0041335C  lea         edi,[ebp-0D8h] 
    00413362  mov         ecx,36h 
    00413367  mov         eax,0CCCCCCCCh 
    0041336C  rep stos    dword ptr es:[edi] 
    const int i = 108;
    0041336E  mov         dword ptr [i],6Ch 
    int *p = (int *)&i;
    00413375  lea         eax,[i] 
    00413378  mov         dword ptr [p],eax  *p = 109;
    0041337B  mov         eax,dword ptr [p] 
    0041337E  mov         dword ptr [eax],6Dh  printf( "address of i:%x\n", (int)&i );
    00413384  mov         esi,esp 
    00413386  lea         eax,[i] 
    00413389  push        eax  
    0041338A  push        offset string "address of i:%x\n" (41584Ch) 
    0041338F  call        dword ptr [__imp__printf (4182B4h)] 
    00413395  add         esp,8 
    00413398  cmp         esi,esp 
    0041339A  call        @ILT+440(__RTC_CheckEsp) (4111BDh) 
    printf( "address of i:%x\n", (int)p );
    0041339F  mov         esi,esp 
    004133A1  mov         eax,dword ptr [p] 
    004133A4  push        eax  
    004133A5  push        offset string "address of i:%x\n" (41584Ch) 
    004133AA  call        dword ptr [__imp__printf (4182B4h)] 
    004133B0  add         esp,8 
    004133B3  cmp         esi,esp 
    004133B5  call        @ILT+440(__RTC_CheckEsp) (4111BDh) 
    printf( "i:%d\n", i );
    004133BA  mov         esi,esp 
    004133BC  push        6Ch  <<----------------------------------注意这里
    004133BE  push        offset string "i:%d\n" (4156ACh) 
    004133C3  call        dword ptr [__imp__printf (4182B4h)] 
    004133C9  add         esp,8 
    004133CC  cmp         esi,esp 
    004133CE  call        @ILT+440(__RTC_CheckEsp) (4111BDh) 
    printf( "*p:%d\n", *p );
    004133D3  mov         esi,esp 
    004133D5  mov         eax,dword ptr [p] 
    004133D8  mov         ecx,dword ptr [eax] 
    004133DA  push        ecx  
    004133DB  push        offset string "*p:%d\n" (4156A4h) 
    004133E0  call        dword ptr [__imp__printf (4182B4h)] 
    004133E6  add         esp,8 
    004133E9  cmp         esi,esp 
    004133EB  call        @ILT+440(__RTC_CheckEsp) (4111BDh)  return 0;
    004133F0  xor         eax,eax 
    }
      

  2.   

    根据反汇编出的代码,可以看到,编译器在处理的时候,会进行优化,并不是从const int i中取值
    而是直接使用常数
      

  3.   

    阴魂不散,老有人搞这种事。
    经过编译器处理后,你的代码相当于这样了:
    cout << "i  = " << 108 << endl;           //i的内容
    cout << "*p = " << *p << endl;          //p指向的内容