大きなプロジェクトで奇妙なセグメンテーション違反に遭遇しました。最後に、コードを見つけてデータをダンプすることができました。以下は単純化されたプログラムです。
#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;
}
ここまで読んでくれてありがとう!問題は、このプログラムにこのデータが提供されている場合、私のマシンで segfault が発生することです (このプログラムがあなたのマシンで動作する場合は、valgrind で実行してみてください)。ただし、コメント化された 2 つのブロックのいずれかをコメント解除すると (つまり、ランダム データを使用するか、最初に配列を並べ替えます)、プログラムは期待どおりに実行されます。
c++03/c++11 の下で、-O2 を使用して/使用せずにコンパイルして、clang 3.3 と gcc 4.8.2 を試しました。それはいつも同じです。archlinux、x64、libstdc++5 3.3.6 を使用しています。
あなたの親切な助けをどうもありがとう:)
segfault バックトレース:
#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
edit : archlinux の場合、gcc 4.8.1-3 にダウングレードすると問題が解決します (sudo pacman -Ud /var/cache/pacman/pkg/gcc-multilib-4.8.1-3-x86_64.pkg.tar.xz)。