Re: Passing a 2 dimensional array from fortran to c++



Sam wrote:
Hello all
I have a two dimensional array (the dimensions are not known)
> that needs to be passed to fortran from c++.

Say in my c++ code I have;

extern "C" { void foo_(float **, int &, int &); }

int main()
{
...........
.............
int rows, columns;
float **array2d; // Now i need to pass this to the fortran code, the
allocation of the array and
                        // and filling it is done in the fortran code.
// How do i pass this to the fortran subroutine now, i did it this way

foo_(array2d, rows, columns);

// One more question, do i need to do the allocation and
> // stuff here again
// Do some stuff to manipulate with the array2d

(snip)

subroutine foo(arr,rows,columns)
integer rows, columns
 real*8 ,allocatable:: arr (:,:)
! do some stuff to allocate the arrray and fil the stuff

First (float**) is not a two dimensional array. It is a pointer to pointer to float. It is often used in place of 2D arrays, but it still
isn't one. Some will even say that a C array declared like:


float x[10][10];

isn't a 2D array, either, but I will disagree.

As far as I know, there is nothing that can be done with a (float**)
in a Fortran program.

The traditional Fortran assumed size array is usually done by passing
the address of the array to the called routine.

Assumed shape arrays must have a descriptor that includes the dimension
information, and also where to find the array elements, possibly in
discontiguous storage.

Calling a Fortran routine with an assumed size argument passing a C pointer should work in many systems. If you really need a (float**)
you can allocate the entire array as one (float*) and then a separate
(float**) to index the columns (or rows) in the C program. Pass the
original (float*) to the Fortran routine.


Much less portably, you could find the form of the descriptor used for
assumed shape arrays and create one as a C struct.  It might be that
a C struct with padding won't do, and that it must be constructed at
runtime with (unsigned char*) and memcpy().  This can probably be
done starting from a (float**).

In all those cases, the array(s) should be allocated using C's malloc().

Another possibility is to find the convention used for ALLOCATABLE by your Fortran system and arrange the C program to use that. In that case it would be allocated in Fortran.

-- glen

.



Relevant Pages

  • Re: Fortran 2003 and F
    ... reason I quit is that I find array declarations without DIMENSION to ... allocate,d) ... When an array is given dimensions such as, ... Fortran programmers know that by default, ...
    (comp.lang.fortran)
  • Re: Who uses clapack?
    ... > functions like SUM and MAXVAL and the ability to use array sections ... > drastically change the meaning of a Fortran code. ... >>Have you ever seen those bills for a Fortran compiler for an ... engineering applications in the C and C++ languages. ...
    (comp.lang.fortran)
  • Re: Who uses clapack?
    ... > functions like SUM and MAXVAL and the ability to use array sections ... > drastically change the meaning of a Fortran code. ... >>Have you ever seen those bills for a Fortran compiler for an ... engineering applications in the C and C++ languages. ...
    (sci.math.num-analysis)
  • Re: writing library functions and pointers?
    ... attempted to adapt the first function I wrote to allocate memory: ... memory to all-bits-zero. ... represents 0.0f or any other valid float value. ... Which I think in this case as I am changing the array it is fine to ...
    (comp.lang.c)
  • Re: Benchmarking APL
    ... with that of FORTRAN. ... APL a 400% gain in programming efficiency could be obtained with only ... Another good-for-APL array problem, vector convolution, gave these ... Tomcatv, one of the SPEC benchmarks, is more typical of large array ...
    (comp.lang.apl)