user3697365
user3697365

Reputation: 61

File reading in C++ error in big files

I am writing an algorithm in C++ which will read a file with numbers with a specific format and then each ( number minus 1 ) that is being read will be the position in an array called Amount and it will be inicreased by 1. The file format is like so

10 5
1 2 3 4 1 5 1 5 2 1

All the values are separated with a space. The first 2 on the first line are the variables N and M and the values from the second line are the ones that will set the position in the array that will be increased. The part of the algorithm for this is :

    int N,M,i;

    FILE *read = fopen("data.in", "r");
    fscanf(read, "%d %d ", &N, &M);
    int Amount[M];
    fill_n(Amount,M, 0); 

    for( i =0; i < N; i++)
    {
        fscanf(read, "%d ", &K);
        Amount[K-1]++;
    }

This algorithm has no problem with a small amount of numbers but I need it to work with up to 1 million numbers, in which case it crashes. What should I change?

Upvotes: 1

Views: 99

Answers (1)

The program crashes because the integer array Amount cannot be allocated enough memory when M goes to a million numbers.

Allocation of memory to local variable amount is from the stack section, which is limited (around 1 MB). That means M could only take values less than 250000.

So, a better approach would be to allocate memory from the heap section by using new[].

int *Amount = new int[M];

Don't forget to de-allocate/return the memory allocated from the heap using delete[] after the usage of amount before exiting the program.

delete[] Amount;

So, after incorporating the mentioned changes, the above code would look as follows:

int N,M,i;
FILE *read = fopen("data.in", "r");
fscanf(read, "%d %d ", &N, &M);
int *Amount = new int[M];
fill_n(Amount,M, 0);  

for(i =0; i < N; i++) {
    fscanf(read, "%d ", &K);
    Amount[K-1]++;
}

//some logic

delete[] Amount;

Upvotes: 1

Related Questions