Re: INTERFACE problem
- From: Gordon Sande <g.sande@xxxxxxxxxxxxxxxx>
- Date: Thu, 31 Mar 2005 19:22:08 GMT
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.)
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".
- Re: INTERFACE problem
- From: Kevin G. Rhoads
- Re: INTERFACE problem