读取二进制文件时出现分段问题。
文件大小为16859136字节,包含双精度值,但实际上我可以读取约4214784字节(少4倍)。
我使用以下c ++代码。
const int size = 6*28*28*28*4*4; // 16859136(bytes) = size * 8(bytes)
double data[size];
ifstream in;
in.open("/path/to/file", std::fstream::binary);
if (in.is_open()) { cout << "File is open!\n"; }
in.read((char *) &data, sizeof(data));
cout << in.gcount() << " bytes read\n";
in.close();
输出:
分段错误
PS:也许存在某种语法错误...
答案 0 :(得分:2)
以下几行在您的堆栈上分配了一个太大的空间:
const int size = 6*28*28*28*4*4; // 16859136(bytes) = size * 8(bytes)
double data[size];
考虑堆分配:
std::vector<double> data(6*28*28*28*4*4);