C++ c抛出访问冲突异常

C++ c抛出访问冲突异常,c++,memory-management,heap,lapack,lapacke,C++,Memory Management,Heap,Lapack,Lapacke,我开发了以下代码,效果很好: #include "header.h" int test_DGGEV_11_14a(){ const int n=3; double a[n][n]={1,1,1,2,3,4,3,5,2}; double b[n][n]={-10,-3,12,14,14,12,16,16,18}; //double a[n][n]={1,7,3,2,9,12,5,22,7}; //double b[n][n]={1,7,3,2,9,12,5

我开发了以下代码,效果很好:

#include "header.h"

int test_DGGEV_11_14a(){
    const int n=3;
    double a[n][n]={1,1,1,2,3,4,3,5,2};
    double b[n][n]={-10,-3,12,14,14,12,16,16,18};
    //double a[n][n]={1,7,3,2,9,12,5,22,7};
    //double b[n][n]={1,7,3,2,9,12,5,22,7};

    /*const int n=2;
    double a[n][n]={1e-16,0,0,1e-15};
    double b[n][n]={1e-16,0,0,1e-15};*/

    lapack_int info;
    double alphar[n]={0.0};
    double alphai[n]={0.0};
    double beta[n]={0.0};
    double vl[n][n]={0.0};
    double vr[n][n]={0.0};

    info=LAPACKE_dggev(LAPACK_ROW_MAJOR,'V','V',n,*a,n,*b,n,alphar,alphai,beta,*vl,n,*vr,n);

    std::cout<<"right eigen vector (what we want):\n";
    for(int i=int(0);i<n;i++){
        for(int j=int(0);j<n;j++){
            printf("%1f ",vr[i][j]);
        }
        printf("\n");
    }
    std::cout<<"left eigen vector:\n";
    for(int i=int(0);i<n;i++){
        for(int j=int(0);j<n;j++){
            printf("%1f ",vl[i][j]);
        }
        printf("\n");
    }
    std::cout<<"eigen values:\n";
    for(int i=int(0);i<n;i++){
        if(beta[i]>DBL_MIN || beta[i]<-DBL_MIN){
            printf("%1f ",alphar[i]/beta[i]);
            printf("\n");
        }else{
            printf("%1f ","beta is zero");
            printf("\n");
        }
    }
    return info;
}
据 LAPACKE_dggev希望将double*作为输入,因此所有矩阵都需要存储为线性阵列

而不是

double**a=NULL;//stiffness
a=new double *[r];
for(int i=int(0);i<r;i++)
    a[i]=new double[c];

所有Other矩阵都有类似的变化。矩阵元素可以作为[i,j]的[i*c+j]来访问。现在,您的代码可能还有其他问题,但这是一个明显的问题,可能会导致您看到的错误。

请发布stacktrace/查看它-它可能会告诉您故障的确切位置occurs@IlyaKobelevskiy我显示了问题的堆栈跟踪。看起来它发生在拉帕克银行里。有趣的是,你在我自己弄明白的时候就发布了你的答案!我希望你昨天把答案贴出来!无论如何,非常感谢。我编辑了我的问题并发布了正确的代码,效果很好。顺便说一句,如果我的代码中有任何错误,请告诉我。是的,我在发布我的答案后看到了你的答案-这是非常精确的时间!我认为代码看起来不错,我唯一的意见是,您可能希望用std::array(如果在编译时知道矩阵大小)或std::vector(如果矩阵大小需要动态)替换矩阵的原始指针,这将减少代码大小,并为您自动管理内存分配,由于程序越来越大,越来越复杂,很难跟踪。谢谢,我将使用std::vector,我的矩阵大小是动态的。
int test_DGGEV_11_18a(){
    const int r=342;
    const int c=342;
    double*a=NULL;//stiffness
    a=new double [r*c];
    for(int i=int(0);i<r*c;i++)
            a[i]=0.0;
    readFile_1Darray("Input_Files/OUTPUT_sub_2_stiffness.txt",a,r,c);
    writeFile_1Darray("Output_Files/K.txt",a,r,c);//to check if readFile was OK

    double*b=NULL;//mass
    b=new double[r*c];
    for(int i=int(0);i<r*c;i++)
            b[i]=0.0;
    readFile_1Darray("Input_Files/OUTPUT_sub_2_mass.txt",b,r,c);
    writeFile_1Darray("Output_Files/M.txt",b,r,c);//to check if readFile was OK

    const int n=r;//r=c=n
    lapack_int info=110;

    //double alphar[n]={0.0};
    double*alphar=NULL;
    alphar=new double[n];
    for(int i=int(0);i<n;i++)
        alphar[i]=0.0;
    //double alphai[n]={0.0};
    double*alphai=NULL;
    alphai=new double[n];
    for(int i=int(0);i<n;i++)
        alphai[i]=0.0;
    //double beta[n]={0.0};
    double*beta=NULL;
    beta=new double[n];
    for(int i=int(0);i++;)
        beta[i]=0.0;
    //double vl[n][n]={0.0};//generates stack overflow
    double*vl=NULL;
    vl=new double[r*c];
    for(int i=int(0);i<r*c;i++)
            vl[i]=0.0;
    //double vr[n][n]={0.0};//generates stack overflow
    double*vr=NULL;
    vr=new double[r*c];
    for(int i=int(0);i<r*c;i++)
            vr[i]=0.0;

    info=LAPACKE_dggev(LAPACK_ROW_MAJOR,'V','V',n,a,n,b,n,alphar,alphai,beta,vl,n,vr,n);
    std::cout<<"info returned by LAPACKE_dggev:\t"<<info<<'\n';

    double*eigValueReal=NULL;
    eigValueReal=new double[n];
    for(int i=int(0);i<n;i++)
        eigValueReal[i]=0.0;
    for(int i=int(0);i<n;i++)
        eigValueReal[i]=alphar[i]/beta[i];

    write1Darray("Output_Files/eigValueReal_LAPACKE_DGGEV.txt",eigValueReal,n);
    write1Darray("Output_Files/beta.txt",beta,n);
    writeFile_1Darray("Output_Files/eigVectorRight_LAPACKE_DGGEV.txt",vr,r,c);

    delete[] a;
    delete[] b;
    delete[] alphar;
    delete[] alphai;
    delete[] beta;
    delete[] vl;
    delete[] vr;
    delete[] eigValueReal;

    return info;
}
double**a=NULL;//stiffness
a=new double *[r];
for(int i=int(0);i<r;i++)
    a[i]=new double[c];
double*a=new double[c*r];//stiffness