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

标签 c++ c++11 stdstring

我遇到了一个我无法弄清楚 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/

相关文章:

c++ - MacOS 创建一个处于挂起状态的进程

c# - 我怎样才能设置 CodeRush 和 Visual Assist X 不相互冲突?

c++ - 带字符串的 RapidJson kArrayType

c++ - 将 uint8 的 vector 转换为字符串

C++ 从堆栈读取 char 到字符串结果为 "Unrecognized enum"

c++ - QList拷贝构造函数:how to new a QList<QStandardItem *> with variable

c++ - 向数组中添加内容

c++ - const static auto lambda 与引用捕获一起使用

c++ - 关于 unique_ptr 中原始指针的类型

c++ - 为什么 std::future 和 std::promise 不是最终的?