Structure handles (C interopability)

IanH
Total Points:
2,270
Status Points:
1,770
Brown Belt
July 8, 2009 6:06 PM PDT
Rate
 
|Best Answer
#3 Reply to #2
Quoting - eos pengwern

So I guess that in this case, as the pointer that has been allocated resides in the module rather than in the 'initialize' subroutine, it stays allocated until explicitly deallocated just like an allocatable array would if held in the module.


I might be missing something here - but the "struct" fortran pointer (it is not an allocatable) being at module scope is not relevant - you could (should? see below) have it as a local variable of each subroutine and you would get the same behaviour.  I think the only reason its been put at module scope here is that it saves a bit of typing. 

I'd prefer "type(pass), pointer :: struct" local to each routine simply because that's the appropriate scope for the lifetime of the information that's associated with that variable.  I also strongly suspect a module scope variable would have issues if this code was ever used in a multi-threaded situation, without additional code, but I don't play in that park.

Each time you call allocate on a pointer, you get a new object, in new storage.  If you want to lose/leak memory, there's nothing stopping you.  This is different to objects with the allocatable attribute.  There's no automatic clean-up/deallocate for pointers either - the programmer needs to look after that (though I think a vendor could offer this as an extension?).  The analogy with c++ is a pointer to an object allocated with new and then nuked with delete.

So normally this sort of setup would have an uninitialise/delete/destruct/free/release function that calls DEALLOCATE.  For every time you "C" client code calls initialise, it must eventually call this cleanup function or the allocated object will sit around until the program terminates.

The attached might be of interest.  It is not necessarily compilable/runable, but hopefully still useful.  Interested in any alternative approaches you might have.

Edit: to fix file attachment problem.


 Attachments 

Intel Software Network Forums Statistics

8470 users have contributed to 31601 threads and 100650 posts to date.
In the past 24 hours, we have 29 new thread(s) 115 new posts(s), and 162 new user(s).

In the past 3 days, the most popular thread for everyone has been gemm(A,A,A) like possible? The most posts were made to gemm(A,A,A) like possible? The post with the most views is Dear Steve, excuse me for a d

Please welcome our newest member kopernikus