Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/140.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ std::n_元件导致SEGFULT;我是否遗漏了什么,或者STL中有一个bug?_C++_Stl_Segmentation Fault - Fatal编程技术网

C++ std::n_元件导致SEGFULT;我是否遗漏了什么,或者STL中有一个bug?

C++ std::n_元件导致SEGFULT;我是否遗漏了什么,或者STL中有一个bug?,c++,stl,segmentation-fault,C++,Stl,Segmentation Fault,我在一个大项目中遇到了一个奇怪的错误;最后,我设法找到了代码并转储了数据。下面是一个简化的程序: #include <cstdlib> #include <vector> #include <iostream> #include <algorithm> using namespace std; const float DATA[] = { 0.179697, -0.413853, -0.079650, 0.167255, -1.26340

我在一个大项目中遇到了一个奇怪的错误;最后,我设法找到了代码并转储了数据。下面是一个简化的程序:

#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;
}
#包括
#包括
#包括
#包括
使用名称空间std;
常量浮点数据[]={
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(){
标准::向量arr(数据,数据+sizeof(数据)/sizeof(数据[0]);
/*
for(std::vector::iterator i=arr.begin();
i!=arr.end();i++)
*i=rand()/(rand_MAX+1.0);
*/
//排序(arr.begin(),arr.end());
std::n_元素(arr.begin(),arr.begin()+1,arr.end());
std::n_元素(arr.begin(),arr.end()-1,arr.end());

您似乎遇到了这个问题,所以您应该至少将libstdc++更新到4.8.3

您知道
arr.begin()+SIZE-1
arr.end()-1
?(即,如果您读取
SIZE
值)你确定你真的从文件中读取了
SIZE
元素吗?没有错误检查。你的程序崩溃的确切位置和堆栈跟踪显示了什么?@Slava请查看新的编辑:)g++4.8.1上的SEGFAULT和clangg++3.4上的SEGFAULT,libstdc++4.8.1segfault仍在g++4.9 2013-10-15中;我现在正在更新它,看看是否已经解决了这个问题。libstdc++包含在gcc的svn repo中。我应该说我是从trunk编译这两个版本的。@DyP关于修复的最后评论发布于2013-10-20是的,这就是我现在编译它的原因,以查看它是否已修复;)gcc/g++4.9 2013-10-22中没有segfault(包括更新的libstdc++);问题确实已修复。
#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