C++ 在返回已销毁变量的地址后,指针现在指向何处? #包括 使用名称空间std; int*JustATest() { int x; x=5+10; return&x; } void main() { int*ptr=JustATest();//返回地址 cout

C++ 在返回已销毁变量的地址后,指针现在指向何处? #包括 使用名称空间std; int*JustATest() { int x; x=5+10; return&x; } void main() { int*ptr=JustATest();//返回地址 cout,c++,C++,指针变为游离指针或悬空指针,取消对它的引用将导致错误 如果您的程序有UB(未定义的行为),则它是格式错误且无效的。还使用术语“悬空指针”。 #include <iostream> using namespace std; int* JustATest() { int x; x = 5 + 10; return &x; } void main() { int * ptr = JustATest(); //return address co

指针变为游离指针或悬空指针,取消对它的引用将导致错误

如果您的程序有UB(未定义的行为),则它是格式错误且无效的。

还使用术语“悬空指针”。
#include <iostream>
using namespace std;
int* JustATest()
{
    int x;
    x = 5 + 10;
    return &x;
}
void main()
{
    int * ptr = JustATest(); //return address
    cout << *ptr << endl; //the output is 15
}