读取向量时出现分段错误
在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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我知道您已经接受了答案,但您发布的代码在以下循环中存在问题:
向量索引是从零开始的。如果向量大小为 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:
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 toAUROC(vector<float> &v)
.当您将函数调用为:
scores
向量将被复制到堆栈中。您不应该通过堆栈传递如此大的数据集合(12 MB),将代码更改为引用或指针传递。此外,您还可以检查和更改主机上的堆栈限制。在 unix 上:
将打印 stacklimit 的当前设置。可以修改一下
,修改后查看一下
When you call your function as:
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:
will print the current setting of stacklimit. You can change it by
and check it after changing
由于它适用于较小的尺寸,因此我最好的猜测是您的堆栈空间不足。如何检查堆栈空间并更改它取决于您的操作系统:在 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 andulimit -s SIZE
to set.Further reading: http://www.learncpp.com/cpp-tutorial/79-the-stack-and-the-heap/
这:
应该是:
和其他地方类似。
This:
should be:
and similar elsewhere.
我想知道你是否可能会弄乱堆栈。
包裹这个
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; }