簡體   English   中英

有內存泄漏嗎?

[英]Is there a memory leak?

我試圖使用Microsoft Visual Studio 2015在C ++中編寫自己的String類。

#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];
}

我嘗試執行此操作時效果很好;

#include<iostream>
#include "MyString.h"
using namespace std;

int main() {
    myString s("my new");
    cout << s+" string" << endl;    
}

但是我不確定char* newContent = new char[newSize];行中的operator+函數是否存在任何內存泄漏char* newContent = new char[newSize]; 我正在從內存中分配新的空間,並且我需要在return語句中return myString(newContent);

因此,我不能在此行之前取消分配它,也不能在return語句之后取消分配它。 我是否正確,是否存在內存泄漏? 如果是這樣,我該如何解決?

編輯1:我已經在Prince Dhaliwal的幫助下更改了operator+函數,如下所示:

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;
}

但是,由於我在本地創建了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"中使用+運算符時。 在您的operator+()定義中,即

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 = new char[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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM