0

I would like to know how to pass a 3d allocatable from Fortran to a C++ function as an argument, and allocate it inside the C++ function. The array will contain the results of computation, then will return to Fortran.

francescalus
  • 30,576
  • 16
  • 61
  • 96
Nouri.M
  • 31
  • 1
  • 4
  • 1
    Please note that tags [tag:fortran-iso-c-binding] and [tag:fortran90] are essentially contradictory. Are you happy using very modern features of Fortran to do this? – francescalus Apr 13 '16 at 16:41
  • You can't allocate `allocatable` array in C++, only a `pointer` array. – Vladimir F Героям слава Apr 13 '16 at 16:53
  • I'm just using gfortran 4.8, with files extension .f90, as I read the Interoperability with C work with this version of fortran compiler. – Nouri.M Apr 13 '16 at 16:53
  • Valdimir F do you have please, any clear example. – Nouri.M Apr 13 '16 at 16:54
  • 1
    BTW .f90 does not mean Fortran 90, it is confusing but it is true. – Vladimir F Героям слава Apr 13 '16 at 16:58
  • 1
    Try http://stackoverflow.com/questions/23891769/allocating-memory-in-c-for-a-fortran-allocatable http://stackoverflow.com/questions/16385372/allocating-memory-in-c-for-a-fortran-array – Vladimir F Героям слава Apr 13 '16 at 16:59
  • In this two example the size of the array is known before calling C functions, In my code the c++ function only know the size of the array after computation, so I can't use static array. – Nouri.M Apr 13 '16 at 17:07
  • [One answer](http://stackoverflow.com/a/16389336) to a question linked by Vladimir F gives much detail. It leaves some to fill in, but there's also another piece of information missing from this question: is the allocatable nature on the Fortran size going to be used, or do you just care about getting something back where you don't know the size ahead of the call (but won't do allocation on the Fortran side later on)? – francescalus Apr 13 '16 at 17:14
  • francescalus,I need the size (array bound) and the contents, in fortran side, I will just use this array to Initialize an other one. – Nouri.M Apr 13 '16 at 17:25
  • Then you can use a `type(c_ptr)` (with extents also coming back) on the interfacing parts, with `c_f_pointer` to give you the local variable in your Fortran procedure, as in IanH's answer. There's also the possibility with TS 29113, but as you don't have a compiler that supports that that wouldn't be a good answer. If you do try with `type(c_ptr)` but come to difficulties then please give more detail. – francescalus Apr 13 '16 at 17:31

0 Answers0