Fitzy
Fitzy

Reputation: 113

C++ Unhandled exception for large vector/array

I keep getting an unhandled exception in my code and it has me stumped.

I am sure it is in the way I have my variables declared.

Basically I am attempting to create 3 arrays, M rows, N columns of random variables. If I set my N = 1,000 and M = 10,000, not a problem. If I then change M = 100,000 I get an Unhandled exception memory allocation error.

Can someone please help me understand why this is happening.

Parts of the code was written on VS2010. I have now moved on to VS2013, so any additional advice on the usage of newer functions would also be appreciated. cheers,

#include <cmath>
#include <iostream>
#include <random>
#include <vector>
#include <ctime>
#include <ratio>
#include <chrono>
int main()
{
    using namespace std::chrono;

    steady_clock::time_point Start_Time = steady_clock::now();

    unsigned int N; // Number of time Steps in a simulation
    unsigned long int M; // Number of simulations (paths)

    N = 1000;
    M = 10000;
// Random Number generation setup 
    double RANDOM;
    srand((unsigned int)time(NULL)); // Generator loop reset

    std::default_random_engine generator(rand()); // Seed with RAND()

    std::normal_distribution<double> distribution(0.0, 1.0); // Mean = 0.0, Variance = 1.0 ie Normal

    std::vector<std::vector<double>> RandomVar_A(M, std::vector<double>(N)); // dw
    std::vector<std::vector<double>> RandomVar_B(M, std::vector<double>(N)); // uncorrelated dz
    std::vector<std::vector<double>> RandomVar_C(M, std::vector<double>(N)); // dz

// Generate random variables for dw
    for (unsigned long int i = 0; i < M; i++)
        {
        for (unsigned int j = 0; j < N; j++)
            {
                RANDOM = distribution(generator);
                RandomVar_A[i][j] = RANDOM;
            }
        }
// Generate random variables for uncorrelated dz
    for (unsigned long int i = 0; i < M; i++)
        {
        for (unsigned int j = 0; j < N; j++)
            {
                RANDOM = distribution(generator);
                RandomVar_B[i][j] = RANDOM;
            }
        }
// Generate random variables for dz
    for (unsigned long int i = 0; i < M; i++)
        {
        for (unsigned int j = 0; j < N; j++)
            {
                RANDOM = distribution(generator);
                RandomVar_C[i][j] = RANDOM;
            }

        }
    steady_clock::time_point End_Time = steady_clock::now();

    duration<double> time_span = duration_cast<duration<double>>(End_Time - Start_Time);
//Clear Matricies
    RandomVar_A.clear();
    RandomVar_B.clear();
    RandomVar_C.clear();

    std::cout << std::endl;
    std::cout << "its done";
    std::cout << std::endl << std::endl;
    std::cout << "Time taken :  " << time_span.count() << " Seconds" << std::endl << std::endl;
    std::cout << "End Of Program" << std::endl << std::endl;
    system("pause");
    return 0;
}
//  *************** END OF PROGRAM ***************

Upvotes: 0

Views: 779

Answers (2)

atkins
atkins

Reputation: 1993

Three 100,000 x 1,000 arrays of doubles represents 300 million doubles. Assuming 8 byte doubles, that's around 2.3 GB of memory. Most likely your process is by default limited to 2 GB on Windows (even if you have much more RAM installed on the machine). However, there are ways to allow your process to access a larger address space: Memory Limits for Windows.

Upvotes: 1

Semyon Burov
Semyon Burov

Reputation: 1172

I'm experienced something similar then my 32-bit application allocates more than 2Gb memory. Your vectors require about 2.1Gb memory, so it might be same problem. Try to change platform of your application to x64. This may solve problem.

Upvotes: 1

Related Questions