C++函数返回局部变量地址,我发现使用vs的release模式则指针位置不变,请问为什么?

在debug模式下:

img

发现第二个cout时*p指向的位置发生了随机的变化,但是在release模式下:

img


却都输出的是10,请问为什么?

#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里面只是存了个地址,你不改变他他不会自己变的