array pointer change after calling FORTRAN subroutine in C++

Qianqian Fang fangqq@gmail.com
Sun Oct 23 17:43:00 GMT 2011


On 10/23/2011 04:53 AM, Paul Richard Thomas wrote:
> Qianqian,
> I suggest that you read the instructions on fortran/C interoperability
> and make sure that you follow them rigorously.
>
> http://gcc.gnu.org/onlinedocs/gfortran/Mixed_002dLanguage-Programming.html#Mixed_002dLanguage-Programming

Thanks for the link. It clarified a lot of things for me.
After reading this, I did not see interfacing a dynamic
array argument. Is it supported?

Also, when I use "iso_c_binding", will the Fortran
.so/.a library file still be linkable with Fortran
programs?

> Failing that, ask your question on comp.lang.fortran.

thanks. I will be heading there for further discussions.

Qianqian

>
> Paul
>
> On Sun, Oct 23, 2011 at 6:52 AM, Qianqian Fang<fangqq@gmail.com>  wrote:
>> hi
>>
>> I am working on a library written in F90 and compiled
>> by gfortran. Now I am trying to write a C++ header to
>> use the fortran library (.so/.a). I am very new to interfacing
>> C/Fortran and found a strange issue when calling the
>> fortran code:
>>
>> Here is a sample of my header file: DBLQMRPrep
>> is a function defined in a fortran module:
>>
>> ...
>> #define DBLQMRPrep      __blit_blqmr_real_MOD_blqmrprep
>>
>> extern void DBLQMRPrep(BLQMRSolver *qmr, int *Ap, double *Ax, int *nz);
>>
>> template<class T>
>> class MyClass{
>>     DBLQMRPrep qmr;
>>     int *Ap;
>>     T *Ax;
>> public:
>>     void Prepare(int *App, T *Axx, int nz){
>>             Ap=App; Ax=Axx;
>> printf("%XL",(unsigned long)Ap);
>>
>>             DBLQMRPrep(&qmr,Ap,Ax,&nz); // here I call FORTRAN
>>
>> printf("%XL",(unsigned long)Ap);
>>     }
>> }
>>
>> You see I put two printf in the Prepare() function,
>> I found that the pointer addresses for Ap/Ax
>> became different after calling DBLQMRPrep().
>>
>> Inside the fortran function, the arrays Ap/Ax are
>> automatic(or dynamic) arrays. Something like:
>>
>>    subroutine BLQMRPrep(this, Ap, Ax, nnz)
>>    implicit none
>>
>>    type(BLQMRSolver), intent(inout) :: this
>>    integer, intent(in) :: nnz, Ap(this%n+1)
>>    MTYPE(kind=Kdouble), intent(in) :: Ax(nnz)
>>    ...
>>
>> Can someone explain to me what happened after
>> calling DBLQMRPrep()? how can I prevent FORTRAN to
>> mess up with the array pointer?
>>
>> thanks
>>
>> Qianqian
>>
>>
>
>



More information about the Fortran mailing list