Feynstein Feynstein - 1 month ago 7
C++ Question

Weird error when reading a large .txt file in c++

i'm trying to read a very large .txt file that has 128x128x128=2097152 lines (linearised 3d space) containing only one 0 or 1 by line (Don't ask why)... I mowed down my code to a few lines and it seems that when I cout the line and the increment, everything goes well... but as soon as I want to put the data inside a sufficiently allowed array, the line reading stops at i=12286...

here's the code

int dim = nbvox[0]*nbvox[1]*nbvox[2];
float* hu_geometry = new float(dim);
int* hu_temp = new int(dim);
string line;

int i = 0;


ifstream in(hu_geom_file.c_str());
if(in.is_open())
{
while(getline(in, line))
{

hu_temp[i] = stoi(line);
cout << "i= " << i << " line= " << line << " hu_temp= " << hu_temp[i] << endl;
i++;
}
cout << __LINE__ << " i=" << i << endl;
in.close();
cout << __LINE__ << endl;
}
else cout << "Unable to open " << hu_geom_file << endl;


Here's the last output I get before getting the error... which is very strange because whenever I comment the hu_temp line inside the while, the cout alone works up to 2097152.

i= 12276 line= 0 hu_temp= 0
i= 12277 line= 0 hu_temp= 0
i= 12278 line= 0 hu_temp= 0
i= 12279 line= 0 hu_temp= 0
i= 12280 line= 0 hu_temp= 0
i= 12281 line= 0 hu_temp= 0
i= 12282 line= 0 hu_temp= 0
i= 12283 line= 0 hu_temp= 0
i= 12284 line= 0 hu_temp= 0
i= 12285 line= 0 hu_temp= 0
115 i=12286
*** Error in `G4Sandbox': free(): invalid pointer: 0x0000000001ba4c40 ***
Aborted (core dumped)

Answer
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)