Allocating memory in C for a Fortran allocatable

后端 未结 3 1509
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-16 06:33

We are trying to take over the memory allocation of a legacy Fortran code (+100,000 lines of code) in C++, because we are using a C library for partitioning and allocating d

3条回答
  •  太阳男子
    2021-01-16 06:55

    Fortran array dummy arguments always start at the lower bound defined in the subroutine. Their lower bound is not retained during the call. Therefore the argument A in TEST() will always start at one. If you wish it to start from 42, you must do:

    INTEGER A(42:*)
    

    Regarding the allocation, you are playing with fire. It is much better to use Fortran pointers for this.

    integer, pointer :: A(:)
    

    You can then set the array to point to a C buffer by

    use iso_c_binding
    
    call c_f_pointer(c_ptr, a, [the dimensions of the array])
    

    where c_ptr is of type(c_ptr), interoperable with void *, which also comes from iso_c_binding.

    ---Edit--- Once I see that @Max la Cour Christensen implemented what I sketched above, I see I misunderstood the output of your code. The descriptor was indeed wrong, though I didn't write anything plain wrong. The solution above still applies.

提交回复
热议问题