当前位置: 首页 > 知识库问答 >
问题:

在std::map中搜索时发生堆栈溢出

公孙宗清
2023-03-14

此代码在运行时由于某种原因导致堆栈溢出异常:

neuralnetwork::CPerceptron::inputEvent(const neuralnetwork::IConnection * origin, double value)
    std::map<std::reference_wrapper<const IConnection>, float64_t, _CPerceptronComparator>::iterator it = m_inputValues.find( std::ref( *origin ) );
    if ( it == m_inputValues.end() )
    {
        throw "Some error";
    }
    ...
}
class _CPerceptronComparator
{
public:
_CPerceptronComparator()
{
}

bool operator()( const std::reference_wrapper<const neuralnetwork::IConnection *> & val1,
                 const std::reference_wrapper<const neuralnetwork::IConnection *> & val2 ) const
{
    return (val1.get()) < (val2.get());
}

bool operator()( const std::reference_wrapper<const neuralnetwork::IConnection> & val1,
                 const std::reference_wrapper<const neuralnetwork::IConnection> & val2 ) const
{
    return &( val1.get() ) < &( val2.get() );
}

bool operator()( const std::reference_wrapper<neuralnetwork::IConnection> & val1,
                 const std::reference_wrapper<neuralnetwork::IConnection> & val2 ) const
{
    return &( val1.get() ) < &( val2.get() );
}
};

我增加了一些代码,也许你能帮上忙。这是我添加输入连接的方式:

 void CPerceptron::addInputConnection( IConnection * inConn )
 {
    m_outConnections.insert( std::ref(*inConn) );

    m_inputValues.insert( std::pair<std::reference_wrapper<IConnection>, float64_t>( std::ref( *inConn ), 0.0f ) );
    m_isInputReady.insert( std::pair<std::reference_wrapper<IConnection>, bool>( std::ref( *inConn ), false ) );
 }

注意:我知道std::make_pair更容易使用,但我替换了它,希望它会引起我的问题。你知道,要看透它返回的类型是不可能的。这个解决方案可能更难读懂,但却是明智之举。

错误:Neural Network.exe中0x002C2EC9处未处理得异常:0xC00000FD:堆栈溢出(参数:0x00000001,0x00102FFC).

Neural Network.exe!std::_Iterator_base12::_Orphan_me() Line 192 C++
Neural Network.exe!std::_Iterator_base12::_Adopt(const std::_Container_base12 * _Parent) Line 165   C++
Neural Network.exe!std::_Iterator_base12::operator=(const std::_Iterator_base12 & _Right) Line 129  C++
Neural Network.exe!std::_Iterator_base12::_Iterator_base12(const std::_Iterator_base12 & _Right) Line 121   C++
Neural Network.exe!std::_Iterator012<std::bidirectional_iterator_tag,std::pair<std::reference_wrapper<neuralnetwork::IConnection const > const ,double>,int,std::pair<std::reference_wrapper<neuralnetwork::IConnection const > const ,double> const *,std::pair<std::reference_wrapper<neuralnetwork::IConnection const > const ,double> const &,std::_Iterator_base12>::_Iterator012<std::bidirectional_iterator_tag,std::pair<std::reference_wrapper<neuralnetwork::IConnection const > const ,double>,int,std::pair<std::reference_wrapper<neuralnetwork::IConnection const > const ,double> const *,std::pair<std::reference_wrapper<neuralnetwork::IConnection const > const ,double> const &,std::_Iterator_base12>(const std::_Iterator012<std::bidirectional_iterator_tag,std::pair<std::reference_wrapper<neuralnetwork::IConnection const > const ,double>,int,std::pair<std::reference_wrapper<neuralnetwork::IConnection const > const ,double> const *,std::pair<std::reference_wrapper<neuralnetwork::IConnection const > const ,double> const &,std::_Iterator_base12> & __that)    C++
Neural Network.exe!std::_Tree_unchecked_const_iterator<std::_Tree_val<std::_Tree_simple_types<std::pair<std::reference_wrapper<neuralnetwork::IConnection const > const ,double> > >,std::_Iterator_base12>::_Tree_unchecked_const_iterator<std::_Tree_val<std::_Tree_simple_types<std::pair<std::reference_wrapper<neuralnetwork::IConnection const > const ,double> > >,std::_Iterator_base12>(const std::_Tree_unchecked_const_iterator<std::_Tree_val<std::_Tree_simple_types<std::pair<std::reference_wrapper<neuralnetwork::IConnection const > const ,double> > >,std::_Iterator_base12> & __that)   C++
Neural Network.exe!std::_Tree_const_iterator<std::_Tree_val<std::_Tree_simple_types<std::pair<std::reference_wrapper<neuralnetwork::IConnection const > const ,double> > > >::_Tree_const_iterator<std::_Tree_val<std::_Tree_simple_types<std::pair<std::reference_wrapper<neuralnetwork::IConnection const > const ,double> > > >(const std::_Tree_const_iterator<std::_Tree_val<std::_Tree_simple_types<std::pair<std::reference_wrapper<neuralnetwork::IConnection const > const ,double> > > > & __that)    C++
Neural Network.exe!std::_Tree_iterator<std::_Tree_val<std::_Tree_simple_types<std::pair<std::reference_wrapper<neuralnetwork::IConnection const > const ,double> > > >::_Tree_iterator<std::_Tree_val<std::_Tree_simple_types<std::pair<std::reference_wrapper<neuralnetwork::IConnection const > const ,double> > > >(const std::_Tree_iterator<std::_Tree_val<std::_Tree_simple_types<std::pair<std::reference_wrapper<neuralnetwork::IConnection const > const ,double> > > > & __that)  C++
Neural Network.exe!std::_Tree<std::_Tmap_traits<std::reference_wrapper<neuralnetwork::IConnection const >,double,neuralnetwork::_CPerceptronComparator,std::allocator<std::pair<std::reference_wrapper<neuralnetwork::IConnection const > const ,double> >,0> >::find(const std::reference_wrapper<neuralnetwork::IConnection const > & _Keyval) Line 1553  C++
Neural Network.exe!neuralnetwork::CPerceptron::inputEvent(const neuralnetwork::IConnection * origin, double value) Line 94  C++

共有1个答案

公孙霖
2023-03-14

问题是由无限循环引起的(实际上是我见过的堆栈溢出的唯一原因)。

void CPerceptron::addInputConnection( IConnection * inConn )
{
    m_outConnections.insert( std::ref(*inConn) );

    m_inputValues.insert( std::pair<std::reference_wrapper<IConnection>, float64_t>( std::ref( *inConn ), 0.0f ) );
    m_isInputReady.insert( std::pair<std::reference_wrapper<IConnection>, bool>( std::ref( *inConn ), false ) );
}

我在m_outConnections中插入了一个名为AddInputConnections的函数,它在图(在神经网络中)中创建了一个圆,因此传播结果会导致无限循环(传播函数不在文章中)。

这是一个很好的例子,说明你很容易错过这么小的东西,但在其他方面却是无用的帖子。IMO删除它。

 类似资料:
  • 我正在处理一个Leetcode问题,可以在这里找到:https://leetcode.com/problems/minimum-distance-between-bst-nodes/ 问题:给定一个具有根节点根的二叉搜索树(BST),返回树中任何两个不同节点的值之间的最小差异。 例子: 输入:root=[4,2,6,1,3, null, null]输出:1解释:注意root是TreeNode对象,

  • 我有一个执行快速排序的应用程序。在我开始给它一些更大的数字(我第一次得到它是10000000)之前,它工作得很好。我知道是由递归引起的,但我不明白为什么我的应用程序会因此而崩溃。如有任何建议,将不胜感激。这是我的密码:

  • 我读了一些关于如何优化正则表达式的文章,但是没有一个答案(更少的组,使用{X,Y}而不是*)似乎阻止了我的正则表达式获得堆栈溢出错误。 我正在尝试通过文件进行动态搜索。假设我正在一个相当大(2-4MB)的文件中搜索“我打赌你找不到我”。我的正则表达式生成器将生成正则表达式: 这个正则表达式的想法是,无论单词之间有什么字符或空格,它都能找到准确的短语。然而,当我尝试使用: 我遇到堆栈溢出错误。我知道

  • 问题内容: 下面给出的代码显示了运行时的Stackoverflow错误。但是,如果我使另一个类CarChange创建Car的对象,它将成功运行。我是一个初学者,请执行以下代码以了解在Java中进行向上转换的重要性。 问题答案: 一个stackoverflow通常意味着您有一个无限循环。 收到此消息的原因是因为您从testdrive方法调用驱动器,并且在该方法中再次调用drive。

  • 问题内容: 我想使用rxjava Observable处理翻新中的分页。我听了另一个问题的建议。 我有100多个页面需要获取,但是链在第20页左右失败,并且在logcat中使用以下日志停止了对可观察对象的任何进一步订阅 有人知道为什么会发生这种情况吗? 更新: 我知道这是由于递归而发生的,但是有没有更优雅的方式来处理带有翻新和rxjava的分页? 问题答案: 因此,鉴于我回答了您引用的原始问题,我

  • 问题内容: 您可以尝试/捕获Java中的堆栈溢出异常吗?它似乎在向任一方向投掷自己。当程序溢出时,我想“惩罚”该值。 问题答案: 似乎可以工作: