I was trying to initialize an array of 1.000.001 element in C++ like this: int array[1000001]. I have 4GB of RAM so I guessed the problem is that my laptop can't hold a so big array because of it's 4 * 1000001 bytes size. So I decided to try to make it char(just because I wanted to know if my guess was right). I am reading the array from a file. This is my code:
#include <iostream>
#include <fstream>
#include <climits>
using namespace std;
int main()
{
fstream in("C:\\Users\\HP\\Documents\\Visual Studio 2017\\Projects\\inputFile.in");
if (!in)
{
cerr << "Can't open input file\n";
return 1;
}
fstream out("outputFile.out", fstream::out);
if (!out)
{
cerr << "Can't open output file\n";
return 1;
}
int n;
in >> n;
int i;
char array[100];
for (i = 0; i < n; i++)
in >> array[i];
in.close();
out.close();
}
For input:
5
45 5 4 3 12
my array is {4, 5, 5, 4, 3}.
For input:
5
12 3 4 5 45
my array is {1, 2, 3, 4, 5}
Now I am really confused. Why is this happening?