我已经尽可能地缩小了范围,这似乎是一个错误......
#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/