#include "stdafx.h"
int main(int argc, char* argv[])
{
const int e = 3;
//! int* v = &e;
int* w = (int*)&e;
*w = 6;
int a;
a = e;
return 0;
}
*w的值与a的值是否相同?
答案为:*w 为6 ; 而a 的值为3。
察看生成的asm文件:
_TEXT ENDS
FLAT GROUP _DATA, CONST, _BSS
ASSUME CS: FLAT, DS: FLAT, SS: FLAT
endif
PUBLIC _main
; COMDAT _main
_TEXT SEGMENT
_e$ = -4
_w$ = -8
_a$ = -12
_main PROC NEAR ; COMDAT
; File E:\code\th_in_c++\const_point\const_point.cpp
; Line 8
push ebp
mov ebp, esp
sub esp, 76 ; 0000004cH
push ebx
push esi
push edi
lea edi, DWORD PTR [ebp-76]
mov ecx, 19 ; 00000013H
mov eax, -858993460 ; ccccccccH
rep stosd
; Line 9
mov DWORD PTR _e$[ebp], 3
; Line 11
lea eax, DWORD PTR _e$[ebp]
mov DWORD PTR _w$[ebp], eax
; Line 12
mov ecx, DWORD PTR _w$[ebp]
mov DWORD PTR [ecx], 6
; Line 14
mov DWORD PTR _a$[ebp], 3
; Line 15
xor eax, eax
; Line 16
pop edi
pop esi
pop ebx
mov esp, ebp
pop ebp
ret 0
_main ENDP
_TEXT ENDS
END
存储在w所指的地址上的数据的确变成了6,但是赋给a的不是存储在w所指的地址上的数据,而是3,即:编译器在处理e时,将e简单的换成了3,进行了常量折叠。。。