【发布时间】:2021-11-18 15:12:26
【问题描述】:
我正在使用取消引用运算符,但我似乎没有得到 x 的值。
#include <iostream>
using namespace std;
class Array {
public:
int* get() {
int x[] = { 31, 45, 39, 32, 9, 23, 75, 80, 62, 9 };
int *r;
r = x;
return r;
}
};
int main() {
Array arr;
int *temp = arr.get();
for (int i = 0; i < 10; ++i) {
cout << temp[i] << " ";
}
}
这打印出 31 32765 0 0 0 0 -1989689609 32624 -989639072 32765 而不是 31 45 39 32 9 23 75 80 62 9
【问题讨论】:
-
这能回答你的问题吗? What is a dangling pointer?
x[]是函数get的本地函数,当它返回时不再存在。