簡體   English   中英

刪除類成員動態分配的內存的最佳方法是什么

[英]What is the best way for deleting dynamic allocated memory of class member

以下代碼只是一個原始示例參考。 一切正常,但是當程序即將關閉時,它卡住了大約幾秒鍾。 這都是因為我假設的刪除內存的錯誤方式。 請找到以下代碼片段:

class Test {
private:
    static int count;
    static Test* arr;
public:
    int num;

    Test() {}

    Test(int val) : num(val) {
        arr[count] = *this;
        count++;
    }

    ~Test() {
        delete[] arr;
    }

    void print() {
        for (int a = 0; a <= count - 1; a++) {
            std::cout << arr[a].num << std::endl;
        }
    }
};

int Test::count = 0;
Test* Test::arr = new Test[2];

int main(int argc, char** argv) {
    Test t1(10);
    Test t2(20);
    t2.print();
    return 0;
}

根本不要使用 raw newdelete

class Test {
private:
    static int count;
    static std::vector<Test> arr;
public:
    int num;

    Test() {}

    Test(int val) : num(val) {
        arr[count] = *this;
        count++;
    }

    // No user-defined destructor needed!

    void print() {
        for (int a = 0; a <= count - 1; a++) {
            std::cout << arr[a].num << std::endl;
        }
    }
};

int Test::count = 0;
std::vector<Test> Test::arr{2};

int main(int argc, char** argv) {
    Test t1(10);
    Test t2(20);
    t2.print();
    return 0;
}

(需要#include<vector>

這做同樣的事情,但不會給您找到正確的delete位置帶來任何麻煩。 std::vector為您管理。

但是請注意,無論此類的目的是什么,它可能不應該限制它可以創建的Test數量:

class Test {
private:
    static std::vector<Test> arr;
public:
    int num;

    Test() {}

    Test(int val) : num(val) {
        arr.push_back(*this); // Add new element!
    }

    // No user-defined destructor needed!

    void print() {
        for (int a = 0; a < arr.size(); a++) {
            std::cout << arr[a].num << std::endl;
        }
    }
};

int Test::count = 0;
std::vector<Test> Test::arr; // Start empty!

int main(int argc, char** argv) {
    Test t1(10);
    Test t2(20);
    t2.print();
    return 0;
}

現在,您不僅限於使用兩個元素,也不必跟蹤count


如果你想使用new (你不應該使用),那么你需要為每個new[]調用一次delete[] new[]

在您的代碼中,您調用new[]一次以在程序啟動時初始化arr 每次銷毀Test時調用delete[]並且程序中有四個Test實例: main 2 個Test類型變量和分配給arr的數組中的 2 個Test類型對象。 這意味着您要刪除arr指向的數組四次! 這會導致未定義的行為。

要正確執行此操作,您需要在不再需要時將其釋放一次

class Test {
private:
    static int count;
    static Test* arr;
public:
    int num;

    Test() {}

    Test(int val) : num(val) {
        arr[count] = *this;
        count++;
    }

    // No user-defined destructor needed!

    void print() {
        for (int a = 0; a <= count - 1; a++) {
            std::cout << arr[a].num << std::endl;
        }
    }
};

int Test::count = 0;
Test* Test::arr = new Test[2]; // Executed once at startup

int main(int argc, char** argv) {
    Test t1(10);
    Test t2(20);
    t2.print();

    delete[] arr; // We don't need the array anymore, so `delete[]` it *once*

    return 0;
}

另請注意,我懷疑您是否正在解決您實際使用此類設計的正確方法的任何問題。 如果您打算注冊在全局注冊表中創建的所有Test類型的對象,那么應該警告您正在arr中保存Test對象的副本

討論解決此類問題的其他方法超出了問題的范圍,尤其是在我不知道實際預期用途的情況下。 所以我會停在這里。

您多次刪除arr的靜態內存,這會導致未定義的行為。

暫無
暫無

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

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