C++ C++;。如何正确释放内存?
在不使用库函数的情况下查找和删除字符串中最大单词的书面代码。一切正常。但是当我想释放内存时,结果是否定的(显示一条空行)。如果取消对内存释放功能的调用,所有功能都将正常工作,但会出现内存泄漏。 我怎么修理它?请帮帮我C++ C++;。如何正确释放内存?,c++,C++,在不使用库函数的情况下查找和删除字符串中最大单词的书面代码。一切正常。但是当我想释放内存时,结果是否定的(显示一条空行)。如果取消对内存释放功能的调用,所有功能都将正常工作,但会出现内存泄漏。 我怎么修理它?请帮帮我 #include <iostream> using namespace std; int length(char *text) // string length { char *begin = text; while(*text++); r
#include <iostream>
using namespace std;
int length(char *text) // string length
{
char *begin = text;
while(*text++);
return text - begin - 1;
}
int size(char **text) // size of two-dimensional array
{
int i = 0;
while(text[i]) i++;
return i;
}
void free_memory(char **text)
{
for(int i=0; i<size(text); i++)
delete text[i];
delete [] text;
}
char **split(char *text, char delim)
{
int words = 1;
int len = length(text);
for(int i=0; i<len; i++)
if(text[i] == delim) words++;
char **result = new char*[words + 1];
int j = 0, t = 0;
for(int i=0; i<words; i++)
{
result[i] = new char[len];
while(text[j] != delim && text[j] != '\0') result[i][t++] = text[j++];
j++;
t = 0;
}
result[words + 1] = nullptr;
return result;
}
char *strcat(char *source, char *destination)
{
char *begin = destination;
while(*destination) destination++;
*destination++ = ' ';
while(*source) *destination++ = *source++;
return begin;
}
char *removeWord(char *in_string)
{
char **words = split(in_string, ' ');
int max = length(words[0]);
int j = 0;
for(int i=0; i<size(words); i++)
if(max < length(words[i]))
{
max = length(words[i]);
j = i;
}
int index;
char *result;
if(!j) index = 1;
else index = 0;
result = words[index];
for(int i=0; i<size(words); i++)
if(i != j && i != index)
result = strcat(words[i], result);
free_memory(words); // I want free memory here
return result;
}
int main()
{
char text[] = "audi and volkswagen are the best car";
cout << removeWord(text) << endl;
return 0;
}
#包括
使用名称空间std;
int-length(char*text)//字符串长度
{
char*begin=text;
而(*text++);
返回文本-begin-1;
}
int size(char**text)//二维数组的大小
{
int i=0;
while(text[i])i++;
返回i;
}
空闲内存无效(字符**文本)
{
对于(int i=0;i您有两个问题。第一个问题是result
被分配到words
中的一个内存位置。第二个问题是,您将strcat
的结果存储在words[i]
中,这可能没有足够的空间(参见文档)
您有两个问题。首先是result
被分配到words
中的一个内存位置。其次,您将strcat
的结果存储在words[i]
中,这可能没有足够的空间(请参阅文档)
实际上,这是C风格的编程——不是C++。我发现你的目标是从头开始实现一切,可能是为了实践。但是即使这样,你的代码也没有被正确地设计/结构化。
除此之外,代码中还有几个bug:
result[words+1]=nullptr;
必须是result[words]=nullptr;
在split
while循环之后,需要result[i][t]='\0';
delete text[i]
必须是delete[]text[i]
您不能从单词中为结果
指针内存赋值,然后释放它,然后返回给调用者使用
在removeWord
的后半部分中,至少还有一个bug。试图理解您试图在那里做什么将是一件乏味的事情
你可能想从一个简单的任务开始。你还应该一步一步的进行检查,首先独立地检查每个函数的正确性,不执行所有的事情,然后进行测试。还看一下ValgRIND工具进行内存检查。如果你使用Linux。< /P> < P>实际上,这是C风格的编程——不是C++。我知道你的目标是IM。从头开始,可能是为了练习。但即使如此,代码的设计/结构也不正确
除此之外,代码中还有几个bug:
result[words+1]=nullptr;
必须是result[words]=nullptr;
在split
while循环之后,需要result[i][t]='\0';
delete text[i]
必须是delete[]text[i]
您不能从单词中为结果
指针内存赋值,然后释放它,然后返回给调用者使用
在removeWord
的后半部分中,至少还有一个bug。试图理解您试图在那里做什么将是一件乏味的事情
您可能希望从一个更简单的任务开始。您还应该一步一步地进行,首先独立检查每个函数的正确性,而不是实现所有功能,然后进行测试。另外,如果您使用Linux,请查看用于内存检查的工具valgrind。正确释放内存的方法是使用RAII:
- 仅在构造函数中使用
new
和new[]
- 将它们与相应析构函数中的
delete
和delete[]
配对
- 尽可能多地使用自动存储持续时间对象
如果您特别不使用std::string
和std::vector
等,出于学习指针的原因,您最终将编写一些类似于string
和vector
和unique\u ptr
的类,然后像使用std
版本一样进行编程分区。正确释放内存的方法是使用RAII:
- 仅在构造函数中使用
new
和new[]
- 将它们与相应析构函数中的
delete
和delete[]
配对
- 尽可能多地使用自动存储持续时间对象
如果您特别不使用std::string
和std::vector
等,出于学习指针的原因,您最终将编写一些类似于string
和vector
和unique\u ptr
的类,然后像使用std
版本一样进行编程与字符数组相比,您更喜欢std::string
。既然您使用的是C++:字符串和向量,为什么不使用“现代”容器呢?这些容器没有内存泄漏。简单的答案是“使用std::string
”。正确但不切实际的答案是“删除new
ed和delete[]
你想做什么new[]
ed”但是,这是太烦人的跟踪,所以只需使用<代码> STD::String 。我将这个问题否决,因为即使完全合理的怀疑这个问题,整个设计完全被搞乱了。任何回答你的问题的答案仍然是非常糟糕的C++代码。如果你想使用这种编程风格的话。如果使用C++,使用所提供的容器,或者至少使用现代设计。如果使用<代码> STD::String < /C> >和“代码> STD::vector < /代码>,大多数代码都将消失。<代码>长度>代码>函数将是<代码> s长度()/代码>,<代码>大小<代码>将是<代码> V siz()。
,不需要空闲内存,strcat
只需
result = new char[len(in_string)+1]; // +1 for space for null char
// the old loop reversed the word order -- if you want to keep doing
// that, make this a descending loop
for(int i=0; i<size(words); i++)
if(i != j && i != index)
strcat(result, words[i]);
free_memory(words);
return result;
int main()
{
char text[] = "audi and volkswagen are the best car";
char * result = removeWord(text);
cout << result << endl;
delete[] result;
return 0;
}