【问题标题】:Returning string arrays from C into Fortran将字符串数组从 C 返回到 Fortran
【发布时间】:2021-09-05 15:12:57
【问题描述】:

我正在尝试使用“iso_c_binding”将字符串数组从 C 返回到 Fortran。该程序编译,但给出了运行时错误。 我的 C 程序:

#include <stdio.h>    
void ret_array(int * numStrings, char **arr2 ) {                                
    int dim1=5;    
    char *arr1[5]={"name1","name2","name3","name4","name5"};    
    arr2 = &arr1;    
    printf("%s\n",arr2[0]);    
    printf("%s\n",arr2[1]);    
    printf("%s\n",arr2[2]);    
    printf("%s\n",arr2[3]);    
    printf("%s\n",arr2[4]);    
    numStrings = &dim1;    
    printf("%s","Ending  interface :");    
    fflush(stdout);    
 }  

我的调用 Fortran 程序

program main
  implicit none
  CHARACTER(LEN = 255), dimension(:), allocatable:: str2
  integer(kind = 4):: istr
  call get_arr(istr, str2)
  PRINT *, str2(1)
contains
  subroutine get_arr(n, str1)
    use iso_c_binding
    implicit none
    INTEGER(KIND = 4):: n
    CHARACTER(LEN = 255), dimension(:), ALLOCATABLE:: str1
    character(kind = c_char), pointer:: fptr(:)
    TYPE(C_PTR), DIMENSION(:), allocatable:: cptr
    integer:: len1, ii

    interface 
      subroutine ret_array(dim_len, str_arr1) bind(c)
        use iso_c_binding
        integer(c_int), INTENT(OUT):: dim_len
        TYPE(C_PTR), DIMENSION(:,:), intent(out):: str_arr1
      end subroutine
    end interface

    call ret_array(n, cptr)
    PRINT *,"Number :",n
    allocate(str1(n))
    do ii = 1, n
      call c_f_pointer(cptr(ii), fptr, [ 255 ])
      len1 = index(fptr(ii), C_NULL_CHAR)
      PRINT *,len1
      str1(ii) = TRANSFER(fptr(1:len1-1),  str1(ii))
    end do
  end subroutine
end program                    

当我运行它时,我收到以下错误。

name1
name2
name3
name4
name5
Ending  interface :
Program received signal SIGFPE: Floating-point exception - erroneous arithmetic operation.

Backtrace for this error:
#0  0x7f9e86776d01 in ???
#1  0x7f9e86775ed5 in ???
#2  0x7f9e865aa20f in ???
#3  0x7f9e869aa077 in ???
#4  0x55e9a5195350 in get_arr
    at /home/test/fort_code/ret_arr.f90:27
#5  0x55e9a5195984 in MAIN__
    at /home/test/fort_code/ret_arr.f90:6
#6  0x55e9a5195a37 in main
    at /home/test/fort_code/ret_arr.f90:7
make: *** [Makefile:3: run] Floating point exception (core dumped)

我不熟悉在 iso_c_binding 中使用指针。您能否指出为什么指针没有返回到 Fortran 程序? 提前感谢您的帮助

【问题讨论】:

  • 您的 c 代码好像发布了两次?
  • 感谢 Timothy G。我已更新到我正在使用的原始 FORTRAN 代码。
  • 首先,您犯了一个典型的 C 错误,即返回指向局部变量的指针,一旦函数返回,该指针将变为指向垃圾的指针 - 请参阅 c-faq.com/malloc/retaggr.html。第二个 Fortran 是小写的,正式使用了 30 多年。最后,您真正想要做什么?如果它只是返回一个字符数组,它看起来非常复杂。
  • 只是为了扩展我对 Fortran 的最后一点,肯定有很多错误,包括至少未能为 cptr 数组分配任何内存并传递一个假定的形状数组(没有做任何新的 f2018 东西)。你能澄清一下你在做什么吗?是否要设置C端的字符串长度?
  • 谢谢伊恩·布什。我正在尝试将字符串数组从 C 返回到 Fortran。我不知道返回的字符串数组的大小和长度。由于开发环境是gcc/gfortran 5.2,所以无法使用f2018。

标签: c pointers fortran fortran-iso-c-binding


【解决方案1】:

您将arr2 更改为指向本地数组arr1,然后返回本地数组的地址。

那是行不通的。函数返回后arr1无效。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-28
    • 1970-01-01
    • 1970-01-01
    • 2013-03-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多