C++ Unhandled exception for large vector/array -
i keep getting unhandled exception in code , has me stumped.
i sure in way have variables declared.
basically attempting create 3 arrays, m rows, n columns of random variables. if set n = 1,000 , m = 10,000, not problem. if change m = 100,000 unhandled exception memory allocation error.
can please me understand why happening.
parts of code written on vs2010. have moved on vs2013, additional advice on usage of newer functions 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 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 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 dw (unsigned long int = 0; < m; i++) { (unsigned int j = 0; j < n; j++) { random = distribution(generator); randomvar_a[i][j] = random; } } // generate random variables uncorrelated dz (unsigned long int = 0; < m; i++) { (unsigned int j = 0; j < n; j++) { random = distribution(generator); randomvar_b[i][j] = random; } } // generate random variables dz (unsigned long int = 0; < m; i++) { (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 ***************
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. process default limited 2 gb on windows (even if have more ram installed on machine). however, there ways allow process access larger address space: memory limits windows.
Comments
Post a Comment