大向量/数组的C ++未处理异常 [英] 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.
基本上,我正在尝试创建3个数组,M行,N列随机变量.如果我将N设置为1,000,将M设置为10,000,这不是问题.如果再将M更改为100,000,则会得到未处理的异常内存分配错误.
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.
部分代码是在VS2010上编写的.我现在进入了VS2013,因此,对使用新功能的任何其他建议也将不胜感激.欢呼,
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 ***************
推荐答案
三个100,000 x 1,000的双打数组表示3亿个双打.假设8字节加倍,则大约有2.3 GB的内存.默认情况下,您的进程很可能在Windows上默认限制为2 GB(即使您在计算机上安装了更多的RAM).但是,有一些方法可以使您的进程访问更大的地址空间:
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.
这篇关于大向量/数组的C ++未处理异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!