gpt4 book ai didi

c++ - 由于可能的松散指针而导致的段错误

转载 作者:太空宇宙 更新时间:2023-11-04 11:44:14 24 4
gpt4 key购买 nike

我有一些关于段错误的问题

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回 (gdb) 向上 #4 0x0000000000402ee3 在 KDTree::Node::Create (this=0x6251c0, coords=0x623ec0, limit=500) 在 KDTree.hxx:64 64 如果( ( *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 );
}
}

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

最佳答案

似乎 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();
}

(对于另一个循环也是如此)。

关于c++ - 由于可能的松散指针而导致的段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20276681/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com