程序终止时堆栈损坏?
Stack Corruption on Program Termination?
我正在为家庭作业编写程序,不太明白我做错了什么。该程序接受一些文本输入,并以 pig latin 输出,虽然程序现在实现了这一点,但程序终止时出现堆栈损坏,VS 提到 "Run-Time Check Failure #2 - Stack around the variable 'token' was corrupted."
我怀疑这是由于不知何故超出了指针的边界,但目前我并不完全理解指针,所以我发现的大多数东西都没有真正意义,而且我是试图理解,而不仅仅是纠正我做错的事情。
附件是使用导致所有问题的变量的函数(经过编辑以包括完整程序,因为我意识到遗漏了一些重要的位)。
int main(void)
{
char text[] = "";
char seps[] = " \t\n";
char *token = NULL;
char *next_token = NULL;
bool cont = true;
bool valid = false;
char contResp;
cout << "Enter a sentence to be translated: ";
do
{
cin.getline(text, 200);
cout << endl << text << endl;
token = strtok_s(text, seps, &next_token);
while (token != NULL)
{
if (token != NULL)
{
printLatinWord(token);
token = strtok_s(NULL, seps, &next_token);
}
}
cout << endl << endl << "Do you want to enter another sentence (y/n)? ";
valid = false;
while (!valid)
{
cin >> contResp;
contResp = tolower(contResp);
if (contResp == 'y')
{
valid = true;
cin.ignore();
cout << "Enter a sentence to be translated: ";
}
else if (contResp == 'n')
{
valid = true;
cont = false;
}
else
{
cout << "Invalid response. Please try again.";
cout << endl << endl << "Do you want to enter another sentence (y/n)? ";
}
}
}
while (cont);
system("pause");
return 0;
}
void printLatinWord(char *token)
{
string text = "";
char *first = token;
token++;
while (*token != '[=10=]')
{
text += *token;
token++;
}
text += *first;
text += "ay ";
cout << text;
}
我不确定如何解决这个问题,但如果我能得到一些帮助以及对我做错了什么的简单解释,我将不胜感激,因为指针算法对我来说大多是胡言乱语.
提前致谢!
char text[] = "";
这将创建一个 1 字节的数组来保存 '[=14=]'
字符(NUL 终止符)。等同于:
char text[1];
text[0] = '[=11=]';
cin.getline(text, 200);
这会将最多 200 个字符(199 个字符加上 NUL 终止符)写入 1 个字符的数组。
显而易见的解决方案:使数组长度为 200 个字符。
char text[200] = "";
或者,对 text
使用 std::string
而不是字符数组,并使用 getline(cin, text);
无限行长度。
我正在为家庭作业编写程序,不太明白我做错了什么。该程序接受一些文本输入,并以 pig latin 输出,虽然程序现在实现了这一点,但程序终止时出现堆栈损坏,VS 提到 "Run-Time Check Failure #2 - Stack around the variable 'token' was corrupted."
我怀疑这是由于不知何故超出了指针的边界,但目前我并不完全理解指针,所以我发现的大多数东西都没有真正意义,而且我是试图理解,而不仅仅是纠正我做错的事情。
附件是使用导致所有问题的变量的函数(经过编辑以包括完整程序,因为我意识到遗漏了一些重要的位)。
int main(void)
{
char text[] = "";
char seps[] = " \t\n";
char *token = NULL;
char *next_token = NULL;
bool cont = true;
bool valid = false;
char contResp;
cout << "Enter a sentence to be translated: ";
do
{
cin.getline(text, 200);
cout << endl << text << endl;
token = strtok_s(text, seps, &next_token);
while (token != NULL)
{
if (token != NULL)
{
printLatinWord(token);
token = strtok_s(NULL, seps, &next_token);
}
}
cout << endl << endl << "Do you want to enter another sentence (y/n)? ";
valid = false;
while (!valid)
{
cin >> contResp;
contResp = tolower(contResp);
if (contResp == 'y')
{
valid = true;
cin.ignore();
cout << "Enter a sentence to be translated: ";
}
else if (contResp == 'n')
{
valid = true;
cont = false;
}
else
{
cout << "Invalid response. Please try again.";
cout << endl << endl << "Do you want to enter another sentence (y/n)? ";
}
}
}
while (cont);
system("pause");
return 0;
}
void printLatinWord(char *token)
{
string text = "";
char *first = token;
token++;
while (*token != '[=10=]')
{
text += *token;
token++;
}
text += *first;
text += "ay ";
cout << text;
}
我不确定如何解决这个问题,但如果我能得到一些帮助以及对我做错了什么的简单解释,我将不胜感激,因为指针算法对我来说大多是胡言乱语.
提前致谢!
char text[] = "";
这将创建一个 1 字节的数组来保存 '[=14=]'
字符(NUL 终止符)。等同于:
char text[1];
text[0] = '[=11=]';
cin.getline(text, 200);
这会将最多 200 个字符(199 个字符加上 NUL 终止符)写入 1 个字符的数组。
显而易见的解决方案:使数组长度为 200 个字符。
char text[200] = "";
或者,对 text
使用 std::string
而不是字符数组,并使用 getline(cin, text);
无限行长度。