繁体   English   中英

为什么会堆栈溢出?

[英]Why does it stack overflow?

我试图通过对角化来解决薛定谔方程。 我在 VS2019 上使用 C++ 并使用 mkl-lapackage 来获取特征值和特征向量。 整个文件是从 LAPACKE_dsyev 的英特尔示例修改而来的。 我只是修改了一小块。 但我遇到了:

0x00C86C79 有一个未处理的异常(在 schro_comp.exe 中):0xC00000FD:堆栈溢出(参数:0x00000000、0x00402000)。

我不知道为什么。 它是 500*500 矩阵。 不大吧? 我请人帮忙。

我的代码是:

#include <stdlib.h>
#include <stdio.h>
#include "mkl_lapacke.h"

/* Auxiliary routines prototypes */
extern void print_matrix(char* desc, MKL_INT m, MKL_INT n, double* a, MKL_INT lda);

/* Parameters */
#define N 500//nstep
#define LDA N
#define RMIN -10.0
#define RMAX 10.0
/* Main program */
int main() {
    /* Locals */
    MKL_INT n = N, lda = LDA, info;

    /* Local arrays */
    double h = (RMAX - RMIN) / (double(N) + 1.0);
    double xi;
    double w[N];
    double a[LDA * N];
    for (int i = 0; i < N; i++) {
        xi = RMIN + double(1.0+i) * h;
        a[i*(N+1)] = 2.0 / h / h+xi * xi;
        if (i==0) {
            a[1] = -1.0 / h / h;
        }
        else if (i == N - 1) {
            a[LDA * N-2] =- 1.0 / h / h;
        }
        else {
            a[i *(N + 1)+1] = 2.0 / h / h + xi * xi;
            a[i * (N + 1) - 1] = 2.0 / h / h + xi * xi;
        }
    }
    /* Executable statements */
    printf("LAPACKE_dsyev (row-major, high-level) Example Program Results\n");
    /* Solve eigenproblem */
    info = LAPACKE_dsyev(LAPACK_ROW_MAJOR, 'V', 'U', n, a, lda, w);
    /* Check for convergence */
    if (info > 0) {
        printf("The algorithm failed to compute eigenvalues.\n");
        exit(1);
    }
    /* Print eigenvalues */
    print_matrix("Eigenvalues", 1, n, w, 1);
    /* Print eigenvectors */
    print_matrix("Eigenvectors (stored columnwise)", n, n, a, lda);
    exit(0);
} /* End of LAPACKE_dsyev Example */

/* Auxiliary routine: printing a matrix */
void print_matrix(char* desc, MKL_INT m, MKL_INT n, double* a, MKL_INT lda) {
    MKL_INT i, j;
    printf("\n %s\n", desc);
    for (i = 0; i < m; i++) {
        for (j = 0; j < n; j++) printf(" %6.2f", a[i * lda + j]);
        printf("\n");
    }
}

我纠正了一些错误,但它仍然存在......

矩阵是这样的:

图片

矩阵的最后一个e_nstep-1应该是e_nstep-2


感谢所有帮助我的人。 问题已经解决了。 VS2019 的栈空间为 1MB。 所以.... malloc()free()可以解决这个问题。

问题已经解决了。 感谢所有帮助我的人。 只需 malloc 和 free 即可解决问题。(VS2019 默认堆栈空间为 1MB,您也可以控制它。)


#include <stdlib.h>
#include <stdio.h>
#include "mkl_lapacke.h"

/* Auxiliary routines prototypes */
extern void print_matrix(char* desc, MKL_INT m, MKL_INT n, double* a, MKL_INT lda);

/* Parameters */
#define N 500//nstep
#define LDA N
#define RMIN -10.0
#define RMAX 10.0
/* Main program */
int main() {
    /* Locals */
    MKL_INT n = N, lda = LDA, info;

    /* Local arrays */
    double h = (RMAX - RMIN) / (double(N) + 1.0);
    double xi;
    double *w;
    double *a;

    w= (double*)malloc(sizeof(double) * N);
    a = (double*)malloc(sizeof(double) * N*LDA);

    for (int i = 0; i < N; i++) {
        xi = RMIN + double(1.0+i) * h;
        a[i*(N+1)] = 2.0 / h / h+xi * xi;
        if (i==0) {
            a[1] = -1.0 / h / h;
        }
        else if (i == N - 1) {
            a[LDA * N-2] =- 1.0 / h / h;
        }
        else {
            a[i *(N + 1)+1] = 2.0 / h / h + xi * xi;
            a[i * (N + 1) - 1] = 2.0 / h / h + xi * xi;
        }
    }
    /* Executable statements */
    printf("LAPACKE_dsyev (row-major, high-level) Example Program Results\n");
    /* Solve eigenproblem */
    info = LAPACKE_dsyev(LAPACK_ROW_MAJOR, 'V', 'U', n, a, lda, w);
    /* Check for convergence */
    if (info > 0) {
        printf("The algorithm failed to compute eigenvalues.\n");
        exit(1);
    }
    /* Print eigenvalues */
    print_matrix("Eigenvalues", 1, n, w, 1);
    /* Print eigenvectors */
    print_matrix("Eigenvectors (stored columnwise)", n, n, a, lda);
    free(a);
    free(w);
    exit(0);
} /* End of LAPACKE_dsyev Example */

/* Auxiliary routine: printing a matrix */
void print_matrix(char* desc, MKL_INT m, MKL_INT n, double* a, MKL_INT lda) {
    MKL_INT i, j;
    printf("\n %s\n", desc);
    for (i = 0; i < m; i++) {
        for (j = 0; j < n; j++) printf(" %6.2f", a[i * lda + j]);
        printf("\n");
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM