Reading in data from file into large array, C++

tacosareveryyummy

Supreme [H]ardness
Joined
Jul 25, 2005
Messages
5,300
I have 2 large files of data that I need to read into array1 and array2. The issue is that when the program reads in array2 it over writes array1 so that both arrays contain the same data. I have a feeling this is due to a memory issue. Can someone take a look and help me out?

Below is the relevant parts of the code..

#include<iostream>
#include<cmath>
#include<fstream>
using namespace std;

double array1[804000][3];
double array2[804000][3];

ifstream inputfile1, inputfile2;

//This function reads in the data from one.txt into array1.
void read1()
{
inputfile1.open("one.txt");
for(int i=0;i<804000;i++)
{
for(int j=0;j<3;j++)
{
inputfile1 >> array1[j];
}
}
inputfile1.close();
}

//This function reads in the data from two.txt into array2.
void read2()
{
inputfile2.open("two.txt");
for(int m=0;m<804000;m++)
{
for(int n=0;n<3;n++)
{
inputfile2 >> array2[m][n];
}
}
inputfile2.close();
}

int main()
{

read1();
read2();

cout << array1[123559][2] << " " << array2[123559][2];

return 0;
}
 
1. Are these just massive text files?
2. Is there a reason you are using fixed size arrays? Personally I would either use pointers (double **array1,array2;) or even better yet, vectors (look up std::vector)
 
Might try dynamically allocating them... those are ~20mb arrays which seems pretty excessive to be statically allocated, I think Visual Studio defaults to limiting your stack to 1mb... which is usually enough for most programs I think.
 
you say the data is the same. try replacing the first read with a array1[j] = 1 and the second with a array2[m][n] =2. Then check the arrays in another loop to see where they break. Since the variables are declared outside of a function they should be globals and not stack variables.
 
Ah yea, forgot about that, I tend to think of memory as off the stack or off the heap, forget about the rest.

It would just crash if he statically allocated too much anyways...

Still, one thing to try is dynamically allocating it, at least then you would know it should be grabbing different pieces of memory.

Code:
  // Allocating 2D array
double** array1 = new double*[804000];
for(unsigned i = 0; i < 804000; ++i)
  array1[i] = new double[3];

array[0][0]; // Use like 2D array, but dynamically allocated

  // deleting your 2D array
for(unsigned i = 0; i < 804000; ++i)
  delete[] array1[i];

delete[] array1;
 
Back
Top