Richard Maine wrote:
> C's malloc acts like a pointer in this regard. Indeed, it does
> return a pointer, albeit a C one. If you are trying to
> emulate malloc, then yes, you'll have to use a pointer.
Yes, I was testing my ISO_C_BINDING emulation and wanted to emulate C malloc
function: I made an f_malloc that returns a C_PTR to a newly allocated (say integer)
array of a given size. And yes, one can use a Fortran array pointer to do the
allocation, but unfortunately one cannot later DEALLOCATE the space back to the
system since the Fortran links to the allocated space have been lost once the pointer
is allocated anew. Fortran has a somewhat rule (strange as in I am not sure how the
processor can inforce it, though I don't think it has to anyway--it is more a user's
responsibility to insure this) that one *can* use a pointer to deallocate a space
through a pointer which was not really used to allocate it, but *only* if it is
associated with the *whole* of the object. So a plain C_PTR cannot be used for the
deallocation as it can in C (with free)--the size must also be carried along, a
pointer made again with C_F_POINTER and then that pointer deallocated.
In any case, this was just a test example and by no means do I want to use to
allocate space in such C-like manners...
Thanks,
Aleksandar
|