NS-VSF.exe中0x76CC1812的未处理异常:Microsoft C ++异常:内存位置0x00AFF5D4的std :: bad_alloc

时间:2019-01-03 12:19:57

标签: c++ dynamic-memory-allocation unhandled-exception

我正在运行代码来解决一些计算问题。当我将域离散化为一个较小的网格(例如16 * 16)时,就不会有问题。但是,一旦我将网格大小增加到121 * 121(使用更多的内存),就会出现此问题:

NS-VSF.exe中0x76CC1812处未处理的异常:Microsoft C ++异常:内存位置0x00AFF5D4处的std :: bad_alloc。

我正在使用动态分配来使用尽可能少的内存(根据我的C ++能力)。我试图在此内存错误发生的函数中使用较少的动态数组。但是同样的问题!

void Poisson(double **A, double **B) {
    double **B_old = new double*[M + 2];
    for (int i = 0; i < M + 2; ++i) B_old[i] = new double[N + 2]();
    double a_x, a_y, a_w;
    a_x = pow(delta_y, 2) / (2 * (pow(delta_x, 2) + pow(delta_y, 2)));
    a_y = pow(delta_x, 2) / (2 * (pow(delta_x, 2) + pow(delta_y, 2)));
    a_w = (pow(delta_x, 2)*pow(delta_y, 2)) / (2 * (pow(delta_x, 2) + pow(delta_y, 2)));

    double convergence = 1.0; //to be updated with L2-norm
    int k = 0; //number of iterations
    while (convergence > 1e-10) {
        for (int i = 2; i < M; i++) {
            for (int j = 2; j < N; j++) {
                B_old[i][j] = B[i][j];
            }
        }
        double sum = 0.0;
        for (int i = 2; i < M; i++) {
            for (int j = 2; j < N; j++) {
                B[i][j] = a_x * (B[i + 1][j] + B[i - 1][j]) + a_y * (B[i][j + 1] + B[i][j - 1]) + a_w * A[i][j];
                B[i][j] = B_old[i][j] + w_SOR *(B[i][j] - B_old[i][j]); //SOR
                sum = sum + pow(B[i][j] - B_old[i][j], );
            }
        }
        convergence = sqrt(sum / (M*N));    // L2-norm criteria
        k++;
        if (k%200 == 0)
        cout << "k= " << k << "..........." << convergence << endl;
    }
    //free memory
    for (int i = 0; i < M + 2; ++i) delete[] B_old[i]; delete[] B_old;
}

0 个答案:

没有答案