这是教授的代码:
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
#include<string>
#include <new>
int main()
{
char *p;
int index = 8;
cout << "Input how many characters:";
cin >> index;
p = new char [index + 1];
cin >> p;
cout << "p is: " << p;
delete [] p;
p = NULL;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在我ANSWER "多少个字符"语句后,一个程序停止了.
谁知道为什么?
Mik*_*our 10
首先你有
cin >> index;
Run Code Online (Sandbox Code Playgroud)
你必须输入字符数.
那你有
cin >> p;
Run Code Online (Sandbox Code Playgroud)
你必须输入一些字符 - 但不超过你之前提供的数字.你在做吗?给出另一个提示可能会有所帮助:
cout << "Input up to " << index << " characters:";
cin >> p;
Run Code Online (Sandbox Code Playgroud)
我希望你的教授能够对缓冲区溢出,输入验证,异常安全以及如何使用std::string以避免手动分配的解释进行跟进.否则,你会被教导一些非常坏的习惯.