【发布时间】:2016-10-31 20:18:21
【问题描述】:
所以我再次尝试学习指针,当我输入一个数字时,Visual Studio 给了我一个错误。 这是来源:
// Includes
#include "stdafx.h"
#include <iostream>
#include <conio.h>
using namespace std;
// Variables
int amount;
// Main function
int main()
{
cout << "How many numbers should be in this array: ";
cin >> amount;
int *p_array;
p_array = new int[amount];
for (int i = 0; i < amount; i++)
{
cout << (int)p_array << endl;
p_array++;
}
delete[] p_array;
_getch();
return 0;
}
我知道p_array++; 有问题。
当我尝试在 Code::Blocks 中编译它时,它运行良好(在代码块中编译时,我删除了 #include "stadfx.h" 并将 _getch(); 更改为 getch();)。
附:我是 C++ 新手:P
【问题讨论】:
-
问题出在
delete[] p_array;这个bug的原因是p_array不再指向使用new分配的地址,因为你在循环中增加了它。 -
@drescherjm 谢谢!我用你的小费修好了。
标签: c++ arrays visual-studio pointers