在debug模式下:
发现第二个cout时*p指向的位置发生了随机的变化,但是在release模式下:
#include <iostream>
using namespace std;
int* func()
{
int a = 10;
return &a;
}
int main()
{
int *p = func();
cout << *p << endl;
cout << *p << endl;
cout << *p << endl;
cout << *p << endl;
cout << *p << endl;
cout << *p << endl;
cout << *p << endl;
cout << *p << endl;
system("pause");
return 0;
}
func返回了一个固定的数值,后面你没有改变p的指向,所以输出一直不变啊
p里面只是存了个地址,你不改变他他不会自己变的