C++ 清除动态阵列中的内存 - 内存无法清除



我在Stack Overflow上阅读了有关 cplusplus.com,tutorialspoint.com,codecall.net 帖子和不同帖子的文章。

我还给我的教授发了电子邮件,在我解释说这些材料不在书中,也不是指定章节的一部分之后,他的回答是:"我很清楚这些材料不在章节中。我希望学生能够做研究。尝试使用 google 搜索指针和 malloc 创建动态数组。我得到了 1,600,000 次点击。

这是我的任务:

使用 STL 堆栈反转读入字符串的输入字符行。 堆栈应包含用户字符串的字符。 使用 getline() 进行输入 – 它需要成为C++工具清单的一部分。

关于getline的说明:假设我正在做以下事情-

This program reverses a string using the STL stack
Enter your string of less than 80 characters followed by an ENTER
a string input
Enter another? 1 = continue. Anything else to stop
1
Enter your string of less than 80 characters followed by an ENTER
a second string

仅使用以下循环将无法正常工作:

int go = 1;
cout << "This program reverses a string using the STL stack" << endl;
while(go == 1){
cout << "Enter your string of less than 80 characters followed by an ENTER" << endl;
char* s = (char *)malloc(80);
cin.getline(s,81,'n');
cout << s << endl;
cout << "Enter another? 1 = continue. Anything else to stop" << endl;
cin >> go;
}

试试吧,看看会发生什么! 另请注意,我从未摆脱过我用 malloc 分配的内存——你的代码必须摆脱它。此外,在循环外使用 malloc 以获得更高效的代码。

你必须在 cin 之后使用 getchar()(cstdio 库的一部分)>> go;

原因是当您输入 1 时,您也使用 Enter 键。 当您再次点击 getline 时,这仍然在缓冲区中! 所以你会读到''

另请注意,当您从 STL 堆栈中获取某些内容时,您必须使用 .top() 查看它,然后使用 .pop() 将其删除。

例:

This program reverses a string using the STL stack
Enter your string of less than 80 characters followed by an Enter
m&m cheeto mayo
oyam oteehc m&m
Enter another? 1 = continue. Anything else to stop
1
Enter your string of less than 80 characters followed by an Enter
celery lettuce apple
elppa ecuttel yrelec
Enter another? 1 = continue. Anything else to stop
0
Press any key to continue . . .

以下是我尝试过的事情:在循环内外使用malloc()。在循环内外同时使用free()delete()。尝试创建一个临时的新指针,然后将我的旧指针重新分配给临时指针。

我一直在使用 repl.it 来完成这项任务,那么这是否可能解释为什么这不起作用?

在我不断运行程序后,看起来内存不清除。它似乎只是将字符添加到我在上次迭代期间键入的内容中。

我希望我涵盖了这篇文章(http://meta.programmers.stackexchange.com/questions/6166/open-letter-to-students-with-homework-problems)中足够的标准。如果我不这样做,我深表歉意。

任何帮助将不胜感激。如果你想为我提供网站的链接,或者可能提供其他主题的研究/谷歌来帮助我。

我的源代码:

#include <iostream>
#include <stdio.h>
#include <stack>
#include <string>
#pragma warning(disable: 4996)
using namespace std;
int main() {
int go = 1;
cout << "This program reverses a string using the STL stack" << endl;
char* s = (char *)malloc(80);   
char *temp;
stack <char> reverse;   

while(go == 1){
cout << "Enter your string of less than 80 characters followed by an ENTER" << endl;
cin.getline(s,81,'n');
for (int i = 0; i < 81; i++){
reverse.push(s[i]); 
}
for (int i = 0; i < 81; i++){
cout << reverse.top();
reverse.pop();
}
char *temp = (char*)realloc(s, 81*sizeof(char));     
if ( temp != NULL )
{
s = temp;
}
else
{
free(s);
}
cout << "nEnter another? 1 = continue. Anything else to stop" << endl;
cin >>go;
getchar();
cout << endl;
}
system("pause");
return 0;
}

这是我的输出:

This program reverses a string using the STL stack
Enter your string of less than 80 characters followed by an ENTER
Joey
yeoJ
Enter another? 1 = continue. Anything else to stop
1
Enter your string of less than 80 characters followed by an ENTER
Dog
goD
Enter another? 1 = continue. Anything else to stop
1
Enter your string of less than 80 characters followed by an ENTER
JoeyDog
goDyeoJ
Enter another? 1 = continue. Anything else to stop
1
Enter your string of less than 80 characters followed by an ENTER
lkjlkdjf;ldjaflkdjaflkdjlfajdlkfja;sdlkjf
fjklds;ajfkldjafljdklfajdklfajdl;fjdkljkl
Enter another? 1 = continue. Anything else to stop
1
Enter your string of less than 80 characters followed by an ENTER
joey
fjklds;ajfkldjafljdklfajdklfajdl;fjdyeoj
Enter another? 1 = continue. Anything else to stop

您不必清除内存。更好的方法是检查字符串的长度。主循环的更正版本(为了清楚起见,我删除了一些不太重要的元素,如 cout):

cin.getline(s,80,'n');
int len = strlen(s);
for (int i = 0; i < len; i++)
reverse.push(s[i]); 
for (int i = 0; i < len; i++){
cout << reverse.top();
reverse.pop();
}

所以我用strlen计算字符串的长度,并在循环中使用结果。请注意,我完全删除了你temp的东西,因为它现在没有任何意义。

最新更新