Hi. I am doing this program checking & comparing the speed of cache of two loops below, and I keep getting error "Automatic allocation exceeds 2G". I am trying to use dynamic array to make this work. Can anyone help me using Dynamic array? I am little lost on that. Here is the code I have.

#include <iostream>
#include <time.h>
using namespace std;

int main()
{
    const int MAX = 100000;
    double A[MAX][MAX],B[MAX][MAX],C[MAX][MAX];

//starting the clock #1
    clock_t start, finish;
    start = clock();

// Initializing array A elements with random numbers (Dinamically Allocate)

// 1st loop
    for(int j=0;j<MAX;j++)
    {
        for(int i=0;i<MAX;i++)
        {
            B[i][j]=i*j;
        }
    finish = clock();   // stop clock #1
    ( (finish - start)/CLOCKS_PER_SEC );

    cout << "Time spent #1 = " << finish << " mili_seconds" << endl;
    }
    cout << '\n' << endl; 

// Marginal codes.....


//starting the clock #2
    clock_t start2, finish2;
    start2 = clock();

// 2nd loop
        for(int j=0;j<MAX;j++)
    {
        for(int i=0;i<MAX;i++)
        {
           C[i][j]=  A[i][j]+B[i][j];
        }
    finish2 = clock();  // stop clock #2
    ( (finish2 - start2)/CLOCKS_PER_SEC );

    cout << "Time spent #2 = " << finish2 - finish << " mili_seconds"<< endl;
    }
}

your arrays that you are making are to large. If you are on a system that has a 8 byte double than each array is going to need approx 75GB of data. To caluculate this you take the rows x colums x size of data type. That gives you 100000 x 100000 = 10,000,000,000 x 8 bytes = 80,000,000,000 bytes. If you change the size to 10,000 x 10,000 than each array would only be approx 800MB. As a side note for large arrays I would suggest creating them dynamiclly at run time so you can use the free store instead of the stack. You can do that like this

//...
const int MAX = 10000;

double ** A, B, C;

A = new double *[MAX];
B = new double *[MAX];
C = new double *[MAX];

for (i = 0; i < MAX; i++)
{
    A[i] = new double[MAX];
    B[i] = new double[MAX];
    C[i] = new double[MAX];
}
//...

Edited 4 Years Ago by NathanOliver

This article has been dead for over six months. Start a new discussion instead.