gpt4 book ai didi

c++ - 使用 Boost Coroutine(1.55) 的段错误不明确?

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:19:51 25 4
gpt4 key购买 nike

我写了一段代码,它会得到一个Segmentation fault。我不确定这是 Boost Coroutine 的错误还是我下面的代码:

#include <string>
#include <functional>
#include <vector>
#include <fstream>
#include <boost/coroutine/coroutine.hpp>
using namespace std;

template <class T>
using C = boost::coroutines::coroutine<T()>;

string foo(C<string>::caller_type & yield,
std::string fn, int cnt) {
std::ifstream f(fn);
// f.close();
int local_cnt = 0;
while(local_cnt < cnt) {
string l;
getline(f, l);
local_cnt ++;
yield(l);
}
f.close();
}

int main(int argc, char *argv[])
{
vector<C<string> > result;
for(int i = 0; i < 8192; ++i) {
C<string> obj(bind(foo, std::placeholders::_1, "test.txt", 3)); // line I
result.push_back(std::move(obj)); // line J
}
return 0;
}

test.txt 非常大,因此它永远不会在段错误发生之前获取 eof。我使用 1.55 的 Boost 并且有一些观察:

  1. 段错误发生在第 I 行
  2. 如果我在 yield 子句之前删除或 move f.close(),段错误就会消失。
  3. 如果我删除代码中的行J,段错误就消失了。
  4. 如果我使用较小的数字代替 8192(比如 512),段错误就会消失。

这里有什么问题?

最佳答案

“打开的文件太多”——您超过了打开文件描述符的最大数量

If I use a smaller number in stead of 8192(say 512), seg-error disappeared.

段错误将在 ifstream 内部发生(不会抛出)检查 ulimit -ncat/proc/sys/fs/文件最大系统控制

关于c++ - 使用 Boost Coroutine(1.55) 的段错误不明确?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28006715/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com