读取向量时出现分段错误

发布于 2024-10-08 01:31:27 字数 787 浏览 1 评论 0原文

在c ++程序中,当我想读取大小为2697806的向量时,我总是收到分段错误错误。 我已经尝试了所有可能的阅读方法:

void AUROC(vector<float> v) {
   ...
   for(std::vector<int>::size_type i = 0; i != v.size(); i++)
      if (v[i]>0) ++pos; else ++neg;

   for(std::vector<long>::size_type i = 0; i != v.size(); i++)
     if (v[i]>0) ++pos; else ++neg;

   for (vector<float>::iterator i=v.begin(); i!=v.end(); ++i)
     if (*i>0) ++pos; else ++neg;

   for (long i=0;i<=v.size();i++)
     if (v[i]>0) ++pos; else ++neg;

   for(int i=0;i<=v.size();i++)
     if (v[i]>0) ++pos; else ++neg;
}

...
int main(void) { 
    vector<float> scores;
    // put data in scores;
    AUROC(scores);
}

对于尺寸小得多的向量,永远不会出现此问题。

感谢您的帮助。 最好的, 佩加

in a c++ program, when I want to read a vector with the size of 2697806, I always get the Segmentation fault error.
I have had tried all possible ways of reading it:

void AUROC(vector<float> v) {
   ...
   for(std::vector<int>::size_type i = 0; i != v.size(); i++)
      if (v[i]>0) ++pos; else ++neg;

   for(std::vector<long>::size_type i = 0; i != v.size(); i++)
     if (v[i]>0) ++pos; else ++neg;

   for (vector<float>::iterator i=v.begin(); i!=v.end(); ++i)
     if (*i>0) ++pos; else ++neg;

   for (long i=0;i<=v.size();i++)
     if (v[i]>0) ++pos; else ++neg;

   for(int i=0;i<=v.size();i++)
     if (v[i]>0) ++pos; else ++neg;
}

...
int main(void) { 
    vector<float> scores;
    // put data in scores;
    AUROC(scores);
}

this problem never happens with the vectors of much smaller sizes.

Thanks for your help.
Best,
Pegah

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

终陌 2024-10-15 01:31:27

我知道您已经接受了答案,但您发布的代码在以下循环中存在问题:

for(int i=0;i<=v.size();i++)

向量索引是从零开始的。如果向量大小为 5,则有效索引为 0..4。您的代码将尝试访问元素 0..5,这是一个差一错误。我相信您对堆栈大小的“修复”只是掩盖了其他实际问题。

此外,您应该通过引用而不是值传递向量。当前,当您调用 AUROC(vectorv) 时,您正在复制向量。这很慢并且极大地浪费了内存。将函数更改为 AUROC(vector&v)

I know you've accepted an answer already but your posted code has a problem with the following loop:

for(int i=0;i<=v.size();i++)

The vector indexes are zero-based. If the vector size is 5 then the valid indexes are 0..4. Your code would try to access elements 0..5 which is an off-by-one error. I believe your "fix" of the stack size is merely masking other real problems.

In addition, you should be passing the vector by reference, not by value. You are currently copying the vector when you call AUROC(vector<float> v). This is slow and an extravagant waste of memory. Change the function to AUROC(vector<float> &v).

不奢求什么 2024-10-15 01:31:27

当您将函数调用为:

vector<float> scores;
// put data in scores;
AUROC(scores);

scores 向量将被复制到堆栈中。您不应该通过堆栈传递如此大的数据集合(12 MB),将代码更改为引用或指针传递。

此外,您还可以检查和更改主机上的堆栈限制。在 unix 上:

ulimit -s 

将打印 stacklimit 的当前设置。可以修改一下

ulimit -s size_in_kb

,修改后查看一下

When you call your function as:

vector<float> scores;
// put data in scores;
AUROC(scores);

The scores vector will be copied into the stack. You should not to pass so large data collections (12 MBytes) via stack, change your code to reference or to pointer passing.

Also, you can check and change stack limits on you Host. On unix:

ulimit -s 

will print the current setting of stacklimit. You can change it by

ulimit -s size_in_kb

and check it after changing

沦落红尘 2024-10-15 01:31:27

由于它适用于较小的尺寸,因此我最好的猜测是您的堆栈空间不足。如何检查堆栈空间并更改它取决于您的操作系统:在 Linux 上运行 ulimit -s 进行检查并运行 ulimit -s SIZE 进行设置。

进一步阅读:http://www.learncpp.com /cpp-tutorial/79-the-stack-and-the-heap/

Since it works for smaller sizes, my best guess is that you're running out of stack space. How to check the stack space and changing it depends on your OS: On Linux run ulimit -s to check and ulimit -s SIZE to set.

Further reading: http://www.learncpp.com/cpp-tutorial/79-the-stack-and-the-heap/

绅刃 2024-10-15 01:31:27

这:

for (long i=0;i<=v.size;i++)

应该是:

for (long i=0;i<=v.size();i++)

和其他地方类似。

This:

for (long i=0;i<=v.size;i++)

should be:

for (long i=0;i<=v.size();i++)

and similar elsewhere.

没有伤那来痛 2024-10-15 01:31:27

我想知道你是否可能会弄乱堆栈。

包裹这个

if (v[i]>0) ++pos; else ++neg;

在花括号中。

{ if (v[i]>0) ++pos;否则++负; }

I wonder if you might be messing with the stack.

Wrap this

if (v[i]>0) ++pos; else ++neg;

in curly braces.

{ if (v[i]>0) ++pos; else ++neg; }

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文