-1

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)
Feynstein
  • 31
  • 7

1 Answers1

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

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
  • I feel stupid right now lol ... but thanks a bunch... this was reviewed by 3 people and worked for a lot of time before posting it on stack... I guess we needed some space from our code... – Feynstein Nov 04 '16 at 16:46
  • this problem happens all the time. Needs a good eye to catch it. Not using arrays at all and vectors instead fixes it definitively. – Jean-François Fabre Nov 04 '16 at 16:47
  • yeah but I find them harder to handle later in my code since this is all mixed with CUDA ... I would normally use vectors but then I have to work with them before sending them to the GPU – Feynstein Nov 04 '16 at 16:52
  • 1
    @Feynstein: Not really; just send `&myVector[0]` to the GPU, more or less as you do now. – Lightness Races in Orbit Nov 04 '16 at 17:24