c++ - dbgheap.c 抛出访问冲突异常

标签 c++ memory-management heap-memory 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,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例程用于大矩阵,修改后的代码如下所示:

#include "header.h"

int test_DGGEV_11_17a(){
    const int r=342;
    const int c=342;
    double**a=NULL;//stiffness
    a=new double *[r];
    for(int i=int(0);i<r;i++)
        a[i]=new double[c];
    readFile("Input_Files/OUTPUT_sub_2_stiffness.txt",a,r,c);
    writeFile("Output_Files/K.txt",a,r,c);//to check if readFile was OK

    double**b=NULL;//mass
    b=new double*[r];
    for(int i=int(0);i<r;i++)
        b[i]=new double[c];
    readFile("Input_Files/OUTPUT_sub_2_mass.txt",b,r,c);
    writeFile("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 alphai[n]={0.0};
    double beta[n]={0.0};
    //double vl[n][n]={0.0};//generates stack overflow
    double**vl=NULL;
    vl=new double*[r];
    for(int i=int(0);i<r;i++)
        vl[i]=new double[c];
    for(int i=int(0);i<r;i++)
        for(int j=int(0);j<c;j++)
            vl[i][j]=0.0;
    //double vr[n][n]={0.0};//generates stack overflow
    double**vr=NULL;
    vr=new double*[r];
    for(int i=int(0);i<r;i++)
        vr[i]=new double[c];
    for(int i=int(0);i<r;i++)
        for(int j=int(0);j<c;j++)
            vr[i][j]=0.0;

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

    return info;
}

在上面修改过的代码中(对于大型矩阵),我必须从堆中分配内存,否则堆栈会溢出。问题是,当我通过 new 从堆中分配内存时,我得到以下与堆相关的异常并发生在 dbgheap.c(调试 CRT 堆函数)中:

enter image description here

有人知道为什么会出现这种异常吗?也许这与 LAPACKE DLL 使用不同的堆进行分配有关……我不知道。


编辑:

堆栈跟踪是这样的:

enter image description here

enter image description here

enter image description here


编辑:

最终通过将所有二维数组替换为一维数组来解决问题。以下代码是更正后的代码,可以正常工作,没有任何错误。有关此解决方案的详细信息,请参阅“Ilya Kobelevskiy”的回答。

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;
}

最佳答案

根据 documentation LAPACKE_dggev 需要 double* 作为输入,因此所有矩阵都需要存储为线性数组。

代替

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

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

关于c++ - dbgheap.c 抛出访问冲突异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26980327/

相关文章:

c++ - 模板作为类中的参数

cocoa - Coredata、EXC_BAD_ACCESS 或相关对象上的奇怪行为

java - 如果 Java 的分代垃圾收集器遍历 Activity 对象图,它们如何知道要对哪些对象调用 finalize()?

windows - _CrtCheckMemory 使用示例

c++ - Box2d cpp 给出 : box2d expression area > 1. 1 异常

c++ - dtor 中的 unique_lock 有什么用处吗?

c++ - 打印不带指数的 gmplib 的 mpf_t 数

ios - 在 iOS 应用程序的生命周期中何时触发 ARC?

c - 在堆栈和堆上分配内存的埃拉托斯特尼筛法的内存错误

android - 由于 eclipse 中的 java 堆空间,无法使用微型 android 应用程序执行 dex