地址未正确传递给Fortran函数调用

时间:2013-09-19 07:32:04

标签: c++ fortran fortran77 arpack

我一直试图通过使用extern“C”在C ++中声明ARPACK函数来找到矩阵的特征向量:

extern "C" {void znaupd_(int *IDO, char *BMAT, int *N, char *WHICH,
                 int *NEV, double *TOL, complex<double> *RESID,
             int *NCV, complex<double> *V, int *LDV,
             int *IPARAM, int *IPNTR, complex<double> *WORKD,
             complex<double> *WORKL, int *LWORKL, 
             double *RWORK, int *INFO);}

extern "C" {void zneupd_(bool *RVEC, char *HOWMNY, bool *SELECT, 
             complex<double> *D, complex<double> *Z,
             int *LDZ, complex<double> *WORKEV, 
             complex<double> *SIGMA, char *BMAT, int *N,
             char *WHICH, int *NEV, double *TOL, 
             complex<double> *RESID, int *NCV, 
             complex<double> *V, int *LDV, int *IPARAM,
             int *IPNTR, complex<double> *WORKD, 
             complex<double> *WORKL, int *LWORKL, int *INFO);}

然后,在我的代码正文中,我调用函数:

  do{
    znaupd_(&IDO, &BMAT, &N, WHICH, &NEV, &TOL, RESID, &NCV, V, &LDV, IPARAM,
        IPNTR, WORKD, WORKL, &LWORKL, RWORK, &INFO);
    switch(abs(IDO)){
    case - 1:
      for(i = 0; i < N; i++) X[i] = WORKD[IPNTR[1] + i];
      gmm::mult(SM, X, Y);
      for(i = 0; i < N; i++) WORKD[IPNTR[2] + i] = Y[i];
      break;
    case 1:
      for(i = 0; i < N; i++) X[i] = WORKD[IPNTR[1] + i]; 
      gmm::mult(SM, X, Y);
      for(i = 0; i < N; i++) 
    {WORKD[IPNTR[2] + i] = Y[i];
      WORKD[IPNTR[3] + i] = X[i];}
      break;
    case 2:
      for(i = 0; i < N; i++)
    WORKD[IPNTR[2] + i] = WORKD[IPNTR[1] + i];
      break;
    }
  }while(IDO != 99);
  std::cout << &INFO << std::endl;
  zneupd_(&RVEC, &HOWMNY, SELECT, D, Z, &LDZ, WORKEV, &SIGMA, &BMAT, &N,
      WHICH, &NEV, &TOL, RESID, &NCV, V, &LDV, IPARAM, IPNTR, WORKD,
      WORKL, &LWORKL, &INFO);

然而,在编译和执行之后,程序会出现段错误。使用GDB执行回溯显示通过&amp; INFO传递给zneupd_的地址是0x0,这会在zneupd_尝试为此位置分配新值时导致段错误。当我向上移动到下一帧并使用print&amp; INFO时,我被告知INFO存储在寄存器0x28a27c中。出于某种原因,我的程序没有正确地将INFO的位置传递给zneupd_。更令人困惑的是,znaupd_能够正确接收&amp; INFO,并且能够毫无问题地访问和修改该位置的值。任何人都可以告诉我为什么一个函数可以正确地接收一个参数,但另一个函数不能?

1 个答案:

答案 0 :(得分:3)

您可能正在阅读错误的ARPACK文档,其中包含ZNEUPD子例程的错误描述。实际上,在RWORK之前还有一个额外的论点INFO(总共24个参数),SIGMA之前还有WORKEV - 参见official documentation和一些source code。在您的情况下,&INFO0只是一个幸运的巧合。

您的代码中还有另一个问题 - 对于x86 / x64上的大多数Fortran编译器,子例程的C原型都是不正确的。这是因为CHARACTER参数BMATWHICHHOWMNY是字符数组(字符串),并且每个实际字符串参数的长度也传递按值作为整数类型的附加隐藏参数。因此ZNAUPD的原型应该是:

extern "C"
void znaupd_(int *IDO,
             char *BMAT,
             int *N,
             char *WHICH,
             int *NEV,
             double *TOL,
             complex<double> *RESID,
             int *NCV,
             complex<double> *V,
             int *LDV,
             int *IPARAM,
             int *IPNTR,
             complex<double> *WORKD,
             complex<double> *WORKL,
             int *LWORKL, 
             double *RWORK,
             int *INFO,
             int _BMAT,    // The length of the actual BMAT argument
             int _WHICH    // The length of the actual WHICH argument
            );

ZNEUPD也是如此,但它有三个隐藏的整数参数:

extern "C"
void zneupd_(bool *RVEC,
             char *HOWMNY,
             bool *SELECT, 
             complex<double> *D,
             complex<double> *Z,
             int *LDZ,
             complex<double> *SIGMA,
             complex<double> *WORKEV, 
             char *BMAT,
             int *N,
             char *WHICH,
             int *NEV,
             double *TOL, 
             complex<double> *RESID,
             int *NCV, 
             complex<double> *V,
             int *LDV,
             int *IPARAM,
             int *IPNTR,
             complex<double> *WORKD, 
             complex<double> *WORKL,
             int *LWORKL,
             double *RWORK,
             int *INFO,
             int _HOWMNY,
             int _BMAT,
             int _WHICH
            );