【问题标题】:memory mangement of list of pointers containning vectors包含向量的指针列表的内存管理
【发布时间】:2015-09-16 13:55:33
【问题描述】:

感谢 Windows 资源监视器,我正在测试此代码以了解如何管理程序内存。

class A {
public:
        string name_;
        vector<double> H;
        vector<float> G;
        vector<string> I;
        int abc;
};
list<A *> test (90000);

void allocate_class() {
    for (list<A *>::iterator it= test.begin(); it != test.end();it++) {
         A *ptr=*it;
         ptr = new A;
    }
}

void init_fct() {
    for (list<A *>::iterator it= test.begin(); it != test.end();it++) {
        A *ptr=*it;
         /* 
        all above doesnt work program crash

        (*ptr).name_ = "hello test";
        ptr->name_ = "hello test";
        ptr->abc = 57; 
        */

    }
}

void erase_class() {
     list<A *>::iterator it= test.begin();
     while( it != test.end() )
         it = test.erase(it);
 }

void delete_ptr() {
    for (list<A *>::iterator it= test.begin(); it != test.end();it++) {
        A *ptr=*it;
        delete ptr;
    }
}

int main()
{
int t;

cout << "before allocation" << endl;
cin >> t;

allocate_class();

cout << "after allocation" << endl;
cin >> t;

init_fct();

cout << "after init" << endl;
cin >> t;

erase_class();

cout << "after erase" << endl;
cout << test.size();
cin >> t;

delete_ptr();
cout << "after delete" << endl;
cout << test.size();
cin >> t;

问题:

  1. 在这种情况下真的需要操作符删除部分吗? (不确定它是否真的可用空间)

  2. init_fct() 中我做错了什么/遗漏了什么?

注意:(属性是故意公开的,cin是暂停程序并检查内存状态)

【问题讨论】:

    标签: c++ pointers memory-management linked-list


    【解决方案1】:
         A *ptr=*it;
         ptr = new A;
    

    这会生成存储在列表中的(未初始化的)指针的副本,然后将指向新分配的@987654322 实例的指针分配给该副本(而不是列表上的指针) @。然后副本超出范围,并且您有内存泄漏。

    当然,由于列表中的指针没有改变,以后使用它们会导致未定义的行为。

    要解决这个问题,请更改列表中的指针:

         *it = new A;
    

    关于您的erase_class 函数:现在是正确的,因为它清除了列表。但是当你在删除列表中的指针之前调用它,你仍然有内存泄漏:

    您首先从列表中删除所有指针,然后尝试删除分配的内存。但是由于您的列表中没有任何指针,因此对其进行迭代将无济于事。

    查看您的目标,检查内存使用情况,这可能是您想要的:

    list<A*> l;
    cin >> dummy;
    // The list will allocate space to hold the pointers:
    l.resize (90000);
    cin >> dummy;
    // Allocate instances for each pointer in the list
    for (auto & ptr : l) {
      ptr = new A{};
    }
    cin >> dummy;
    // Free the allocated instances
    for (auto & ptr : l) {
      delete ptr;
    }
    cin >> dummy;
    // Erase all elements from the list.
    // Note that this is not necessary, when
    // the list goes out of scope at the end
    // of main this is done by the list destructor.
    // Moreover, there's a function to erase
    // all elements from the list:
    //  l.clear();
    auto it = l.begin();
    while (it != l.end()) {
      it = l.erase(it);
    }
    cin >> dummy;
    

    请注意,使用智能指针(或者在这种情况下根本没有指针)作为列表的元素可以让您免于手动删除分配的内存的负担。

    【讨论】:

    • 好的,谢谢,现在看来函数 init_fct() 陷入了无限循环并继续显示“hello test”
    • 请显示修改后的代码。不过,打印 90000 行 hello world 不会立即发生。
    • 哦,对不起,我忘记了我设置了 90000 行,这没关系,正常,它需要太长时间,我认为这是一个无限循环
    • 那个擦除功能也不正确,稍后会编辑我的答案。
    • 我发现了错误,我的想法是如果使用 .erase() 方法,我不应该增加迭代器,因为我想删除所有项目而不增加我编辑我的问题并更正 erase_class() 函数谢谢让我知道
    猜你喜欢
    • 1970-01-01
    • 2015-05-19
    • 1970-01-01
    • 2018-08-28
    • 2018-05-30
    • 2013-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多