有内存泄漏吗? 我尝试使用微软Visual Studio 2015在C++中编写自己的字符串类。我写的课是这样的 #include<string.h> class myString { private: char* content; public: int size; myString(); myString(char*); ~myString(); bool operator== (const myString &) const; bool operator!= (const myString &) const; myString operator= (const myString &); myString operator+ (const myString &) const; myString operator+= (const myString &); friend std::ostream& operator<< (std::ostream &os, const myString &); char operator[] (int &) const; }; std::ostream& operator<<(std::ostream &os, const myString &string) { os << string.content; return os; } myString::myString() { size = 0; content = "\0"; } myString::myString(char* newContent) { size = strlen(newContent); content = new char[size+1]; strcpy(content, newContent); } myString::~myString() { delete[] content; } myString myString::operator= (const myString &string) { if (size != string.size) { delete[] content; size = string.size; content = new char[size+1]; } strcpy(content, string.content); return *this; } bool myString::operator== (const myString &string) const { if (size != string.size) return false; if (strcmp(content, string.content)) return false; return true; } bool myString::operator!= (const myString &string) const { if (*this == string) return false; return true; } myString myString::operator+ (const myString &string) const { int newSize = size + string.size; char* newContent = new char[newSize]; strcpy(newContent, content); strcat(newContent, string.content); return myString(newContent); } myString myString::operator+= (const myString &string) { *this = *this + string; return *this; } char myString::operator[] (int &index) const { return content[index]; }

有内存泄漏吗? 我尝试使用微软Visual Studio 2015在C++中编写自己的字符串类。我写的课是这样的 #include<string.h> class myString { private: char* content; public: int size; myString(); myString(char*); ~myString(); bool operator== (const myString &) const; bool operator!= (const myString &) const; myString operator= (const myString &); myString operator+ (const myString &) const; myString operator+= (const myString &); friend std::ostream& operator<< (std::ostream &os, const myString &); char operator[] (int &) const; }; std::ostream& operator<<(std::ostream &os, const myString &string) { os << string.content; return os; } myString::myString() { size = 0; content = "\0"; } myString::myString(char* newContent) { size = strlen(newContent); content = new char[size+1]; strcpy(content, newContent); } myString::~myString() { delete[] content; } myString myString::operator= (const myString &string) { if (size != string.size) { delete[] content; size = string.size; content = new char[size+1]; } strcpy(content, string.content); return *this; } bool myString::operator== (const myString &string) const { if (size != string.size) return false; if (strcmp(content, string.content)) return false; return true; } bool myString::operator!= (const myString &string) const { if (*this == string) return false; return true; } myString myString::operator+ (const myString &string) const { int newSize = size + string.size; char* newContent = new char[newSize]; strcpy(newContent, content); strcat(newContent, string.content); return myString(newContent); } myString myString::operator+= (const myString &string) { *this = *this + string; return *this; } char myString::operator[] (int &index) const { return content[index]; },c++,string,class,memory,memory-leaks,C++,String,Class,Memory,Memory Leaks,但是由于我在本地创建了temp,它在返回它之前调用它的析构函数并给出错误。我想我也应该为temp分配内存。我改变了函数如下 myString myString::operator+ (const myString &string) const { myString* temp= new myString; int newSize = size + string.size; char* newContent = new char[newSize+1]; te

但是由于我在本地创建了
temp
,它在返回它之前调用它的析构函数并给出错误。我想我也应该为temp分配内存。我改变了函数如下

myString myString::operator+ (const myString &string) const {
    myString* temp= new myString;
    int newSize = size + string.size;
    char* newContent = new char[newSize+1];
    temp->size = newSize;
    strcpy(newContent, content);
    strcat(newContent, string.content);
    temp->content = newContent;
    return *temp;
}
它现在工作正常,但我相信仍然存在内存泄漏,因为
temp
变量。如果内存泄漏,如何修复

编辑2:
我只是通过创建一个复制构造函数来修复它,实际上代码中存在内存泄漏。使用
s+“string”
中的
+
运算符时。在您的
操作符+()
定义中,即

myString myString::operator+ (const myString &string) const {
    int newSize = size + string.size;
    char* newContent = new char[newSize];
    strcpy(newContent, content);
    strcat(newContent, string.content);
    return myString(newContent);
}
您正在这里分配新字符串
char*newContent=newchar[newSize],将旧零件和新零件复制到新字符串。再次在构造函数
return myString(newContent)中分配新字符串。但是你在哪里删除你的旧字符串?它不在你的代码中。因此,您必须删除字符串
newContent
。 你可以这样做

myString myString::operator+ (const myString &string) const {
    myString temp;
    int newSize = size + string.size;
    char* newContent = new char[newSize + 1];
    temp.size = newSize;
    strcpy(newContent, content);
    strcat(newContent, string.content);
    temp.content = newContent;
    return temp;
}
更新 您必须创建一个副本构造函数

myString(const myString &rhs) :
size(rhs.size) {
    content = new char[size + 1];
    strcpy(content, rhs.content);
}

分析你的代码,然后你就知道是否有内存泄漏。如果是这样的话,你可以试着找到在哪里。为什么你会这样做,当STL有一个字符串类?实际上这是从我的家庭作业,他们禁止我们使用它们。我想是为了学习。是的,正如您所描述的,内存泄漏。哦,我明白了,您还没有创建复制构造函数
myString(const myString&)
。实际情况是,默认复制构造函数将执行成员复制,这意味着复制和原始指针
content
都指向同一内存。当调用
temp
的析构函数时,该内存被删除。感谢您的回答!顺便问一下,当我们为newContent分配内存时,我们不应该写newSize+1吗?因为“\0”字符?此外,我们在本地创建了临时字符串。所以当这个操作符+函数完成时,它调用temp的析构函数。我应该如何处理这个问题?在调用
temp
的析构函数之前,它将在返回局部变量副本时被复制,这很好。我使用了Visual Studio的调试器。在返回行中,在返回temp之前,它将执行temp的deconstructor,清除内容并返回一个没有内容的字符串,当我试图打印其内容时,它会给出错误。我们应该使用newSize+1表示
\0
。我在代码中做了更改。
myString myString::operator+ (const myString &string) const {
    myString temp;
    int newSize = size + string.size;
    char* newContent = new char[newSize + 1];
    temp.size = newSize;
    strcpy(newContent, content);
    strcat(newContent, string.content);
    temp.content = newContent;
    return temp;
}
myString(const myString &rhs) :
size(rhs.size) {
    content = new char[size + 1];
    strcpy(content, rhs.content);
}