//============================================================================
// Name : Constant_folding.cpp
// Author : gwwu
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
/* 1. i,j 地址相同,指向同一块空间
* 2. i,j指向同一块内存,但是*j = 1修改内存后,
* 按理说*j == 1, i 也应该==1,但是实验结果是为0.
* 因为i是可折叠常量,在编译阶段对i的引用已经替换为i的值了
* 也就是说 cout << i << endl; 在编译后就被替换为
* cout << 0 << endl;*/
#include <iostream>
using namespace std;
int main() {
const int i = 0;
int *j = (int*)&i;
*j = 1;
cout << "&i = " << &i <<endl;
cout << "j = " << j << endl;
cout << "i = " << i <<endl;
cout << "*j = " << *j << endl;
return 0;
}
编译运行:
&i = 0x28ff28
j = 0x28ff28
i = 0
*j = 1