我遇到有关段错误的问题
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)
在/usr/include/c++/4.6/bits/STL_queue.h:165
165返回c.front();
(gdb)向上
KDTree::Node::Create中的#4 0x0000000000402ee3(this = 0x6251c0,coords = 0x623ec0,limit = 500)at KDTree.hxx:64
64 if((* itC)-> front()> maxAbove)maxAbove =(* itC)-> front();
这是一段代码
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 );
}
}
我怀疑是因为完成时会丢失最初的指针,但是,我不知道该问题的任何解决方案,有什么想法吗?
最佳答案
似乎itC
是this -> 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();
}
(对于其他循环也是如此)。
关于c++ - 段错误,可能是指针松动,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20276681/