为什么堆栈会溢出?

问题描述

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

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,w,1);
    /* Print eigenvectors */
    print_matrix("Eigenvectors (stored columnwise)",lda);
    exit(0);
} /* End of LAPACKE_dsyev Example */

/* Auxiliary routine: printing a matrix */
void print_matrix(char* desc,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");
    }
}

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

矩阵类似于:

Image

矩阵的最后一个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,w,1);
    /* Print eigenvectors */
    print_matrix("Eigenvectors (stored columnwise)",lda);
    free(a);
    free(w);
    exit(0);
} /* End of LAPACKE_dsyev Example */

/* Auxiliary routine: printing a matrix */
void print_matrix(char* desc,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");
    }
}

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...