C++ Unhandled exception for large vector/array

ε祈祈猫儿з 提交于 2021-02-11 15:35:58

问题


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 ***************

回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/32865482/c-unhandled-exception-for-large-vector-array

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!