【问题标题】:destructor is called multiple number of times while inserting into map插入地图时多次调用析构函数
【发布时间】:2013-06-24 18:26:09
【问题描述】:

在下面的代码中,析构函数被多次调用:-

#include<iostream>
#include<map>
using namespace std;


class abc{

    public:
        abc()
        {
            cout<<"\nconstructor called\n";
        }

        ~abc()
        {
            cout<<"\ndestructor called\n";
        }
        void fun()
        {
            cout<<"\nfunction called\n";
        }
};

struct cmp_str
{
   bool operator()(abc a,abc b)
   {
      return 1;
   }
};




int main()
{

map<abc,int,cmp_str> mymap;
abc a;
mymap[a]=5; // destructor is called twice
//mymap.insert(pair<abc,int>(a,5)); // // destructor is called 3 times

map<abc,int,cmp_str>::iterator it=mymap.begin();
mymap.clear();
while(1)
{
//infinite loop added to check number of times destrctor is called before objects goes out of scope
}
return 1;
}

我正在使用不同的方式在对象中插入值。 一个使用 insert() 函数,另一个使用简单的 []。 当我使用 mymap[a]=5 插入时;然后析构函数被调用两次,而当我评论这一行并使用 insert() 函数进行插入时,析构函数被调用三次。 由于 mymap.clear() 可以忽略一个析构函数,但为什么要调用其余的析构函数。

我在 return 上方插入了无限循环,这样我就可以忽略当对象超出范围时调用的析构函数。 请帮助我理解这种行为,因为多次调用析构函数是危险的,如果处理不当可能会导致核心转储。

【问题讨论】:

  • 当您使用insert() 时,还有一个,因为调用该函数时会涉及到一个复制构造函数。
  • @AlexandruBarbarosie:实际上,我相信我们正在看到被破坏的是临时 pair 对象中的对象。

标签: c++ c++11 map destructor


【解决方案1】:

您的比较函子按值接受其参数,因此会制作和销毁临时副本。

但是,由于 as-if 规则的复制省略例外,很难预测将存在的临时对象的确切数量。这将取决于您的优化器有多好以及是否所有内容都被内联。

【讨论】:

  • 但是在执行程序时只调用一次构造函数。即使创建了临时对象,它也应该显示多个构造函数,但这不会发生。
  • @user1057741 将abc(abc const&amp;) { cout&lt;&lt;"\n copy constructor called\n"; } 添加到您的代码中。届时一切都会揭晓。
  • Praetorian: 谢谢 :) .. 明白了
【解决方案2】:

在比较运算符中通过引用捕获,而不是创建新对象......

bool operator()(const abc& a, const abc& b)

【讨论】:

  • 我做了,但它仍在创建一些临时对象...可能是地图的一些内部功能。
猜你喜欢
  • 2012-07-12
  • 2014-08-22
  • 2018-12-04
  • 1970-01-01
  • 2010-12-29
  • 1970-01-01
  • 2012-08-06
  • 1970-01-01
相关资源
最近更新 更多