c++ - libc++ std::search_n 中的崩溃是一个错误吗?

标签 c++ c++11 stl libc++

我已经尽可能地缩小了范围,这似乎是一个错误......

#include <algorithm>
#include <vector>

int main(int argc, char *argv[])
{
  // Crashes
  std::vector<uint8_t> bs{1, 0, 0};
  std::search_n(bs.begin(), bs.end(), 3, 1);

  // Does not crash
  std::vector<uint8_t> bs{1, 0};
  std::search_n(bs.begin(), bs.end(), 2, 1);

  return 0;
}

我明白了

Segmentation fault: 11

我希望我没有错误地使用 std::search_n :)

目前使用 LLDB 似乎不可能逐步完成 STL 实现。

版本信息:

$clang --version
Apple LLVM version 4.2 (clang-425.0.24) (based on LLVM 3.2svn)
Target: x86_64-apple-darwin12.3.0
Thread model: posix

证据;)

13:06:47 ~/bug$ cat bug.cc
#include <algorithm>
#include <vector>

int main(int argc, char *argv[])
{
  std::vector<uint8_t> bs{1, 0, 0};
  std::search_n(bs.begin(), bs.end(), 3, 1);

  // std::vector<uint8_t> bs{1, 0};
  // std::search_n(bs.begin(), bs.end(), 2, 1);

  return 0;
}
13:06:52 ~/bug$ clang++ -std=c++11 -stdlib=libc++ bug.cc -o bug
13:07:36 ~/bug$ ./bug
Segmentation fault: 11
13:07:42 ~/bug$

最佳答案

这似乎是 search_n 中的一个错误,它对我来说也崩溃了(Xcode 4.6.1)。我认为在 __search_n 测试中

if (__first == __s)  // return __last if no element matches __value_

需要

if (__first >= __s)  // return __last if no element matches __value_

发生的事情是算法开始匹配,然后不匹配并重新开始;这个新的起点超出了 __s,这是模式长度的逻辑上最后可能的起点。旧试只考平等,不考“超越”。通过修复,它不再对我崩溃。

关于c++ - libc++ std::search_n 中的崩溃是一个错误吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15809929/

相关文章:

c++ - 有没有办法删除传递给函数 `foo (new Object())` 的对象?

C++ STL : Passing an empty container to lower_bound

c++ - 通过在另一组上调用 erase(iterator) 从一组中删除元素。这是正常行为吗?

c++ - 在 `std::get<I>` 上使用 `std::tuple` 是否保证对于 `I` 的不同值是线程安全的?

c++ - STL 对和二进制搜索

c++ - [c++]循环在 692 处停止

c++ - 如何让 Cmake 包含 gl/GL.h?

c++ - 如何执行高效的集合交集操作?

c++ - 使用 std::map 的 Miniheap lambda

c++ - 在线程之间共享数据