Re: INTERFACE problem





Kevin G. Rhoads wrote:
Thank your for your answer. I know "low-level" very good , so an idea
to use integers as pseudo-pointers was the first idea I came to. But
the problem is more complex. I have an integer at Fortran side, pass
it by reference to C function, treat here as pointer, allocate memory
and this C-pointer at Fortran side now is an integer which has
represents address allocated at C side. Very good.

But really C-side function allocated an array. C pointers and arrays
have the same syntax, so if I allocated p = malloc(...), I can use
p[i]. But now I pass this pointer to Fortran side, there it a simple
INTEGER and how to treat is an array ? How to set array values ?
Memory is allocated, how to access it by index ?


Any way of doing this will be non-portable and will require knowledge
of data representations of the underlying implementations for the C and
the Fortran (which probably, but not necessarily, correspond to one or
more "native" forms on the hardware).

I did something like this many years ago to allow dynamic allocation
for Fortran.  The routine was developed and debugged with fixed length
arrays.  Then it was converted into a subroutine with the arrays and
their lengths passed in as arguments.  The Main was a short Fortran
stub which passed integers to Pascal (no C on that platform at that time)
to get dynamic allocations -- same use of integers as pseudo-pointers
as you've discussed.  WHen the Main got the now initialized "pointers"
back, it called the sub by way of Pascal with those "pointers".  The
pascal routine was just a shell which dereferenced the pointers and
passed the addresses to the Fortran subroutine which was expecting arrays
in those slots.  This was an abuse of the fact that in that implementation,
Fortran arrays were passed by passing the address of the array beginning.

It is a gnarly unportable hack, but it is sometimes useful. (Or more
correctly, I got the data analysis programs working and got my PhD,
and since the programs were tied to the data acquisition computer,
the non-portability was a fairly non-issue.)

YMMV

An old hack used to get dynamic allocation in Fortran is to use whatever allocator you want and let it also figure out the apparent subscript of the dynamic storage with respect to an array in common. The subdivision of arrays for use as temporary storage is a moderately common usage in Fortran. The hack here is to make the allocation more idiomatic in Fortran by returning the apparent subscript rather than some "pointer".

.



Relevant Pages

  • Re: Speed penalty for using allocatable arrays?
    ... "Leading dimension" means dimension 1, ... Fortran compilers, rather than having being left those writing Fortran ... allocation available in Fortran 90/95. ... the arrays are allocated once at the ...
    (comp.lang.fortran)
  • Re: Fortran memory allocation (stack/heap) issues
    ... > rather than Fortran, ... dynamic allocation, and relatively little stack allocation. ... value return and arrays by reference. ...
    (comp.lang.fortran)
  • Re: Cracking DES with C++ is faster than Java?
    ... > C doesn't have multidimensional arrays, ... you must use pointers to pointers instead, ... properties from Fortran (efficient code, separate compilation, ... However C didn't borrow object orientation ...
    (comp.lang.java)
  • Re: Cracking DES with C++ is faster than Java?
    ... > C doesn't have multidimensional arrays, ... you must use pointers to pointers instead, ... properties from Fortran (efficient code, separate compilation, ... However C didn't borrow object orientation ...
    (comp.lang.cpp)
  • Re: Cracking DES with C++ is faster than Java?
    ... > C doesn't have multidimensional arrays, ... you must use pointers to pointers instead, ... properties from Fortran (efficient code, separate compilation, ... However C didn't borrow object orientation ...
    (sci.crypt)