gpt4 book ai didi

c++ - std::string::empty() const () 不抛出段错误

转载 作者:太空宇宙 更新时间:2023-11-04 15:40:27 25 4
gpt4 key购买 nike

我遇到了一个我无法弄清楚 atm 的特殊问题:

  for ( const auto & p : _patterns )
{
auto it = std::find_if( p->Tokens().begin(),p->Tokens().end(),
[&]( const Token & lhs )
{
return ( lhs == query );
});

if ( it != p->Tokens().end() )
d_freq++;
}

代币是:

std::vector<Token>

std::find_if lambda 内部的实际运算符被定义和实现为:

bool Token::operator== ( const Token & rhs  ) const
{
if ( !this->_value.empty() && !rhs._value.empty() )
{
return boost::iequals( this->_value, rhs._value );
}
else
throw
std::runtime_error ( "[Token::operator==] empty string" );
}

运行我的调试版本会因 SEGFAULT 而崩溃。

仅当我运行 gdb,然后回溯时,我得到:

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff721b173 in std::string::empty() const () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
(gdb) backtrace
#0 0x00007ffff721b173 in std::string::empty() const () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#1 0x00000000005b082d in Token::operator== (this=0xc35818, rhs=...) at /Token/Token.cpp:37
#2 0x00000000005ee12a in TFIDF::Calculate(std::vector<Token, std::allocator<Token> >, Token)::{lambda(Token const&)#1}::operator()(Token const&) const (__closure=0x7fffffffd840, lhs=...)

我觉得这很奇怪,因为如果我理解正确的话,它应该抛出一个很好的异常,但它没有。

更有趣的是,就在我调用它崩溃的方法 (TFIDF::Calculate) 之前,我使用相同的标记、相同的 std::find_if 和完全相同的 lambda 进行了非常相似的搜索,但它没有'崩溃!

我显然遗漏了一些东西,所以有人可以帮忙吗?

最佳答案

作为stated in the comments , Tokens()返回 std::vector<Token> 按值。这意味着下面的代码将比较 beginend两个不同的迭代器 vector s,这是未定义的行为。

auto it = std::find_if( p->Tokens().begin(),p->Tokens().end(),
[&]( const Token & lhs )
{
return ( lhs == query );
});

要解决此问题,请修改 Tokens()以便它返回 std::vector 通过引用,或将返回值存储在变量中。

auto tokens = p->Tokens();
auto it = std::find_if( tokens.begin(), tokens.end(),
[&]( const Token & lhs )
{
return ( lhs == query );
});

作为@WhozCraig suggests , 这可以进一步简化为

auto tokens = p->Tokens();
auto it = std::find(tokens.begin(), tokens.end(), query);

if ( it != tokens.end() ) // don't use p->Tokens() here either!! :)
d_freq++;

关于c++ - std::string::empty() const () 不抛出段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24319329/

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