我在一个大项目中遇到了一个奇怪的段错误;最后我设法找到代码并转储数据。这是一个简化的程序:
#include <cstdlib>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
const float DATA[] = {
0.179697, -0.413853, -0.079650, 0.167255, -1.263407, 1.707440, -0.162176,
-0.176349, -0.826179, -0.097582, -0.265471, 0.070675, 0.077035, -0.218272,
-0.509723, -0.244462, 0.000000, -0.069970, -0.169399, 0.236123, -1.063037,
0.048428, 0.080877, -0.099672, -0.580204, -0.174694, -0.082321, -0.313485,
1.828802, -0.110842, -0.367741, 0.026412, 0.116269, -0.164420, -0.726286,
-0.335257, 0.456737, -0.465721, -0.242003, -0.755520, -1.155553, 0.013372,
-0.033874, -0.105618, 0.000000, -0.578532, -0.057074, 0.026309, -0.978317,
-0.253747
};
int main() {
std::vector<float> arr(DATA, DATA + sizeof(DATA) / sizeof(DATA[0]));
/*
for (std::vector<float>::iterator i = arr.begin();
i != arr.end(); i ++)
*i = rand() / (RAND_MAX + 1.0);
*/
// std::sort(arr.begin(), arr.end());
std::nth_element(arr.begin(), arr.begin() + 1, arr.end());
std::nth_element(arr.begin(), arr.end() - 1, arr.end());
cout << arr.back() << endl;
}
感谢您在这里阅读!问题是,如果提供此数据,此程序将在我的机器上发生段错误(如果它在您的机器上运行,您可以尝试使用valgrind运行它);但是,如果我取消注释任何注释的两个块(即使用随机数据,或首先对数组进行排序),程序将按预期运行。
在c ++ 03 / c ++ 11下,我尝试过使用clang 3.3和gcc 4.8.2,使用/不使用-O2进行编译;它总是一样的。我使用archlinux,x64,libstdc ++ 5 3.3.6。
非常感谢您的帮助:)
segfault backtrace:
#0 0x0000000000401a9f in std::__unguarded_partition<__gnu_cxx::__normal_iterator<float*, std::vector<float, std::allocator<float> > >, float> (
__first=<error reading variable: Cannot access memory at address 0x625000>, __last=1.89120642e-40, __pivot=@0x6040c8: 1.82880199)
at /usr/include/c++/4.8.2/bits/stl_algo.h:2242
#1 0x0000000000401497 in std::__unguarded_partition_pivot<__gnu_cxx::__normal_iterator<float*, std::vector<float, std::allocator<float> > > > (__first=1.82880199,
__last=1.89120642e-40) at /usr/include/c++/4.8.2/bits/stl_algo.h:2283
#2 0x0000000000401134 in std::__introselect<__gnu_cxx::__normal_iterator<float*, std::vector<float, std::allocator<float> > >, long> (__first=1.82880199,
__nth=0.236122996, __last=1.89120642e-40, __depth_limit=7) at /usr/include/c++/4.8.2/bits/stl_algo.h:2365
#3 0x0000000000400e48 in std::nth_element<__gnu_cxx::__normal_iterator<float*, std::vector<float, std::allocator<float> > > > (__first=-0.174694002, __nth=0.236122996,
__last=1.89120642e-40) at /usr/include/c++/4.8.2/bits/stl_algo.h:5377
#4 0x0000000000400b2c in main () at tnew.cc:29
编辑:对于archlinux,降级到gcc 4.8.1-3解决了这个问题(sudo pacman -Ud /var/cache/pacman/pkg/gcc-multilib-4.8.1-3- x86_64.pkg.tar.xz)