由于松散指针可能出现分段错误

时间:2013-11-29 00:31:43

标签: c++

我遇到了一些有关段故障的问题

Program received signal SIGSEGV, Segmentation fault.
0x0000000000403a62 in std::_Deque_iterator<float, float&, float*>::_Deque_iterator (this=0x7fffffffc5c0, __x=...)
    at /usr/include/c++/4.6/bits/stl_deque.h:136
136         _M_last(__x._M_last), _M_node(__x._M_node) { }
(gdb) up
#1  0x0000000000403a0f in std::deque<float, std::allocator<float> >::begin (this=0x64) at /usr/include/c++/4.6/bits/stl_deque.h:1010
1010          { return this->_M_impl._M_start; }
(gdb) up
#2  0x000000000040326f in std::deque<float, std::allocator<float> >::front (this=0x64) at /usr/include/c++/4.6/bits/stl_deque.h:1286
1286          { return *begin(); }
(gdb) up
#3  0x000000000040248c in std::queue<float, std::deque<float, std::allocator<float> > >::front (this=0x64)

at /usr/include/c++/4.6/bits/stl_queue.h:165     165 return c.front();     (gdb)了     #4 0x0000000000402ee3在KDTree :: Node :: Create(这= 0x6251c0,coords = 0x623ec0,limit = 500)在KDTree.hxx:64     64 if((* itC) - &gt; front()&gt; maxAbove)maxAbove =(* itC) - &gt;前();

这是一段代码

template< class T, class D >
void KDTree< T, D >::Node::
Create( Coords* coords, D limit )
{
    Coords* newCoordsBelowMedian = new Coords(); 
    Coords* newCoordsAboveMedian = new Coords();
    D maxAbove = 0,
    minAbove = 0,
    maxBelow = 0,
    minBelow = 0;
    this -> m_Coords = coords;
    this -> m_Median = GetMedian( *coords );
    typename Coords :: iterator itC = this -> m_Coords -> begin( ); 
    //Change of coordinates for next iteration
    for( ; itC != this -> m_Coords -> end( ); itC++ )
    {
        Dims* newDim = *itC;
        D value = newDim -> front( );
        newDim -> pop( );
        newDim -> push( value );
        if( newDim -> front() >= this -> m_Median )   newCoordsAboveMedian -> insert(  );
        else                                          newCoordsBelowMedian -> insert(  );
    }  

    typename Coords :: iterator itCA = newCoordsAboveMedian -> begin( );
    typename Coords :: iterator itCB = newCoordsBelowMedian -> begin( );

    minBelow = std::numeric_limits<D>::max();
    minAbove = std::numeric_limits<D>::max();
                    //Max radius
    for( ; itC != newCoordsAboveMedian -> end( ); itCA++ )                             
    {
      if( ( *itC ) -> front() > maxAbove ) maxAbove = ( *itC ) -> front();
      if( ( *itC ) -> front() < minAbove ) minAbove = ( *itC ) -> front();
    }
    for( ; itC != newCoordsBelowMedian -> end( ); itCB++ )
    {
        if( ( *itC ) -> front() > maxBelow ) maxBelow = ( *itC ) -> front();
        if( ( *itC ) -> front() > maxBelow ) minBelow = ( *itC ) -> front();
    }

    if( abs( maxAbove - minAbove ) < limit && newCoordsAboveMedian -> size() > 0 )
    {
        this -> m_R = new Node();
        this -> m_R -> Create( newCoordsAboveMedian, limit );
    }
    if( abs( maxBelow - minBelow ) < limit && newCoordsAboveMedian -> size() > 0 )
    {
        this -> m_L = new Node();
        this -> m_L -> Create( newCoordsBelowMedian, limit );
    }       
}    

我怀疑是因为第一个指针在完成时丢失了,但是,我不知道这个问题的任何解决方案,任何想法?

1 个答案:

答案 0 :(得分:1)

似乎itCthis -> m_Coords的迭代器,它在第一个循环中运行到最后。相同的迭代器用于控制后面的循环。你的意思是这个循环

 for( ; itC != newCoordsAboveMedian -> end( ); itCA++ )                             
            {
              if( ( *itC ) -> front() > maxAbove ) maxAbove = ( *itC ) -> front();
              if( ( *itC ) -> front() < minAbove ) minAbove = ( *itC ) -> front();
            }

阅读

 for( ; itCA != newCoordsAboveMedian -> end( ); itCA++ )                             
            {
              if( ( *itCA ) -> front() > maxAbove ) maxAbove = ( *itCA ) -> front();
              if( ( *itCA ) -> front() < minAbove ) minAbove = ( *itCA ) -> front();
            }

......或者,如果我要写这个循环,我会怎么做:

  for (typename Coords::iterator it = newCoordsAboveMedian->begin( ),
                                 end = newCoordsAboveMedian->end();
       it != end; ++it) {
              if( ( *it ) -> front() > maxAbove ) maxAbove = ( *it ) -> front();
              if( ( *it ) -> front() < minAbove ) minAbove = ( *it ) -> front();
            }

(同样适用于其他循环)。

相关问题