C ++ std :: vector的指针删除和分割错误 [英] C++ std::vector of pointers deletion and segmentation faults

查看:243
本文介绍了C ++ std :: vector的指针删除和分割错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个指向一个类的指针的向量。我需要调用他们的析构函数并释放他们的记忆。因为它们是指针的向量vector.clear()不做任务。我继续手动做这样:

  void Population :: clearPool(std :: vector< Chromosome *> a,int size)
{
染色体*
for(int j = 0; j< size-1; j ++)
{
c = a.back();
a.pop_back();
delete c;
printf(%d \\\
\r,j);
c = NULL;

}

}

因为我有一个说话的析构函数,看看在哪个染色体的分割错误发生。当clearPool()被调用,并说我们的大小为100,它可以给出在0和100之间的任何染色体的分段错误。



我不知道为什么可能发生,也没有办法真正找到什么问题,因为在调试的断点,我看到的是发生在那里随机染色体。



我使用代码块IDE和gdb调试器。当分段错误发生时,堆栈跟踪具有4个内存地址和一个函数 wsncpy()

解决方案

  void Population :: clearPool(std :: vector< Chromosome *>& a)
{
for(int i = 0; i delete a [i];
}
a.clear();
}

请注意,向量通过引用传递。在代码中,使用向量的副本,这意味着在调用程序中它不会改变。因为你删除了副本中的指针,原来的指针现在都是无效的 - 我怀疑你使用那些无效的指针,在你发布的代码中没有显示。



由于已经发布了使用C ++库算法的几个模板解决方案,您可能还需要考虑一个模板解决方案,它不会:

  template< class C> void FreeClear(C& cntr){
for(typename C :: iterator it = cntr.begin();
it!= cntr.end(); ++ it){
删除它;
}
cntr.clear();使用它可以释放任何动态分配对象的容器:

使用这个函数可以释放任何动态分配的对象的容器:



 载体<染色体*> vc; 
list< Chromosome *> lc;
// populate&使用
FreeClear(lc);
FreeClear(vc);


I have a vector of pointers to a class. I need to call their destructors and free their memory. Since they are vector of pointers vector.clear() does not do the job.So I went on to do it manually like so :

void Population::clearPool(std::vector<Chromosome*> a,int size)
{
    Chromosome* c;
    for(int j = 0 ;j < size-1;j++)
    {
       c = a.back();
       a.pop_back();
       delete c;
       printf("  %d \n\r",j);
       c = NULL;

    }

}

The printf in there is since I have a talking destructor to see in which Chromosome the segmentation fault happens. When clearPool() is called and say we got a size of 100, it can give a segmentation fault in any Chromosome between 0 and 100.

I have no idea why this might be happening nor do I have a way to actually find what's wrong since while debugging with breakpoints all I see is that it happens in there at random chromosomes.

I am using codeblocks IDE and the gdb debugger. The stack trace when the segmentation fault happens has 4 memory addresses and a function wsncpy().

解决方案

void Population::clearPool( std::vector <Chromosome*> & a )
{
   for ( int i = 0; i < a.size(); i++ ) {
      delete a[i];
   }
   a.clear();
}

Notice that the vector is passed by reference. In your code, a copy of the vector is used, which means that it is unchanged in the calling program. Because you delete the pointers in the copy, the pointers in the original are now all invalid - I suspect you are using those invalid pointers in some way not shown in the code you posted.

As a couple of template solutions have been posted that use C++ library algorithms, you might also want to consider a template solution that does not:

template <class C> void FreeClear( C & cntr ) {
    for ( typename C::iterator it = cntr.begin(); 
              it != cntr.end(); ++it ) {
    	delete * it;
    }
    cntr.clear();
}

Using this you can free any container of dynamically allocated objects:

vector <Chromosome *> vc;
list <Chromosome *> lc;
// populate & use
FreeClear( lc );
FreeClear( vc );

这篇关于C ++ std :: vector的指针删除和分割错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆