| Thread Tools | Search this thread |
|---|
eos pengwern
| July 7, 2009 3:52 PM PDT Structure handles (C interopability) | ||||
Chapter 14 of Metcalf, Reid and Cohen describes the following code sample ("Figure 14.5"): ...and comments: "When the C code calls initialize, it passes the size. The Fortran code allocates a structure and inside it stores the size and allocates an array component; it then returns a pointer to the structure. The C code later calls add and passes additional data together with the pointer that it received from initialize. The Fortran procedure add uses c_f_pointer to establish a Fortran pointer for the relevant structure. Note that C may call initialize several times if it wishes to work simultaneously with several problems; each will have a separate structure of type pass and be accessible through its own 'handle' of type (c_ptr)." Well and good; the explanation is clear enough, and this is exactly what I want to do. Looking at the code, though, makes me feel uneasy, as it's not obvious (a) how the compiler ensures that memory occupied by a previously-defined pointer is not overwritten, as no variables here have the 'save' attribute, or (b) assuming that they are somehow automatically saved, how then does one deallocate them later and so avoid a memory leak? Will a simple deallocate(struct) do the trick? In C++ and other languages, this situation is a little clearer because one creates a named object, based on a class, one can store the object (or a pointer to it) in an array, and knows to call the object destructor when one is done. Here, things don't look so safe: I picture a number of disembodied structs floating around in memory with nothing to keep track of them except some handles that, one hopes, the C application is looking after properly. Am I being too pessimistic here, or are there some other elements I need to add to the template to ensure that nothing gets overwritten or lost? Thank you, Stephen. | |||||
|
|||||||||||||
|
|||||||||||||
|
|||||||||||||
|
|||||||||||||
| 8285 users have contributed to 31229 threads and 99107 posts to date. |
|---|
| In the past 24 hours, we have 7 new thread(s) 35 new posts(s), and 47 new user(s). In the past 3 days, the most popular thread for everyone has been comparison cilk++, openmp, pthreads first results The most posts were made to comparison cilk++, openmp, pthreads first results The post with the most views is Very amusing... Escalated as Please welcome our newest member tvinni |