Weird error when reading a large .txt file in c++
This question already has an answer here:
float* hu_geometry = new float(dim);
int* hu_temp = new int(dim);
those are 1-char arrays containing the value dim
. At some point you're hitting a MMU boundary and crashes randomly.
You want to write:
float* hu_geometry = new float[dim];
int* hu_temp = new int[dim];
or maybe better with vectors, pre-allocated with dim
elements
#include <vector>
std::vector<float> hu_geometry(dim);
std::vector<int> hu_temp(dim);
or not allocated at start:
std::vector<int> hu_temp;
and in your code:
hu_temp.push_back(stoi(line));
( hu_temp.size()
gives the size and a lot of very nice features better described here)
上一篇: 我有segfaults!