- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I have a fortran routine, which is calling a C++ routine. The C++ routine will return an array of structures and the number of elements in the array.
My issue is that I would like to dynamically allocate memory for the array, however I do not know the size prior to calling the C++ routine.
Any suggestions?
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I guess your C routine will, in fact, allocate memory for the array using MALLOC or whatever way is usual for C (I'm not a C phreak). Then it returns with its argument set to the address of the array, the usual C-style pointer. So your task is to use that pointer and make something on the Fortran side understand it. To me that sounds like it needs a CRAY-pointer, i.e. a pointer created with a POINTER statement (not a pointer attribute).
I won't go into any further deatils because, to be honest, I'm on rather shaky ground here, and your post doesn't really give sufficient information (notice the first 2 words above). However if you look up cray pointers in IVF help you should find it useful.
Perhaps someone else has a firmer opinion?
Qolin
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
This is very typical of Win32 programming, where the canonical arguments passed from the opsys to user proc functions frequently refer to Win32 data structures. Here is a typical proc function, in F90, which (further on in the code) processes messages with extra information in an nmhdr structure, passed as an address via lParam:
[bash]INTEGER FUNCTION ConflictProc (hwnd, msg, wParam, lParam) RESULT (res) !DEC$ IF DEFINED(_X86_) !DEC$ ATTRIBUTES STDCALL, ALIAS : '_ConflictProc@16' :: ConflictProc !DEC$ ELSE !DEC$ ATTRIBUTES STDCALL, ALIAS : 'ConflictProc' :: ConflictProc !DEC$ ENDIF IMPLICIT NONE INTEGER(HANDLE), INTENT(IN) :: hwnd INTEGER(UINT), INTENT(IN) :: msg INTEGER(fWPARAM), INTENT(IN) :: wParam INTEGER(fLPARAM), INTENT(IN) :: lParam TYPE(T_NMHDR) :: nmhdr POINTER(lParam, nmhdr) [/bash]
and then the F90 part of the code simply uses nmhdr%component as needed.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Portable, F2003 std compliant example that uses the approach alluded to by tim18.
C++ determines the size of the array, allocates the memory, returns a C_PTR and the size of the array (via a pass-by-reference parameters/arguments) back to Fortran. Fortran associates the returned c-ptr with a standard fortran POINTER, then does some useful work. When finished, it calls back into C++ so that C++ can cleanup (this time passing the C_PTR by value).

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page