繁体   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