为什么它会堆栈溢出?

Why does it stack overflow?

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

0x00C86C79 has an unhandled exception (in schro_comp.exe): 0xC00000FD: Stack Overflow (parameter: 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上stack的space是1MB。所以....malloc()free()可以解决这个问题。

问题已解决。感谢所有帮助我的人。 malloc和free解决问题。(VS2019栈space默认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");
    }
}