有人可以解释为什么在循环内声明变量时指针会被覆盖吗?
例如,给出以下代码片段,并且用户输入1和2。我希望pNums
数组包含2个指向2个分别保持值1和2的整数的指针。
但是,控制台会打印出2
和2
;
#include <iostream>
using namespace std;
//Input "1 2"
int main() {
int* pNums[2];
for(int i = 0; i < 2; i++){
int num;
cin >> num;
pNums[i] = (&num);
}
cout << (*pNums[0]) << endl;
cout << (*pNums[1]) << endl;
}
为什么会这样?我该如何解决呢?例如,如果我们不知道用户要输入多少个数字,又有一个for
循环,而不是while
循环,该怎么办?在满足某些条件之前,我们要继续创建新的指针并将其存储到pNums
向量中?
只有一个num
,并且您正在覆盖that。(然后导致未定义的行为,但是不要介意。)
有两种简单的方法可以避免此错误。
1)存储对象,而不是指针:
int nums[2];
for(int i = 0; i < 2; i++){
cin >> nums[i];
}
2)使用动态分配:
int* pNums[2];
for(int i = 0; i < 2; i++){
int *p=new int;
cin >> *p;
pNums[i] = p;
}
您存储在pNums
中的指针指向num
块中变量for
的两个实例。在每个for
循环迭代中都有一个变量实例,并且这些变量仅在到达for
循环体的各自迭代结束时才有效。
因此,当for
循环退出时,您的指针将无效,因此尝试使用例如*pNums[0]
导致undefined behavior。
不存储指针,存储值:
#include <iostream>
using namespace std;
//Input "1 2"
int main() {
int pNums[2];
for(int i = 0; i < 2; i++){
int num;
cin >> num;
pNums[i] = num;
}
cout << pNums[0] << endl;
cout << pNums[1] << endl;
}
并且如果您需要数组中可变数量的条目,请使用std::vector
。
for(int i = 0; i < 2; i++){
int num; //< this is num. It lives here.
cin >> num;
pNums[i] = (&num); //< you have taken the address of num (twice!)
}
// here is when 'num' is destroyed (no longer in scope)
// so this is now pointing at something that doesn't exist.
cout << (*pNums[0]) << endl;