- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I have a question concerning preprocessor definitions. I wrote the following code.
Using the preprocessor definition "my_allocate" I can allocate any derived type. I could of course simply use "allocate". For other reasons I want to write my own allocation routines. Now, my problem is that I cannot call the subroutine (i.e. preprocessor definition) "my_allocate" from outside the module. How can I implement a "regular" subroutine which is as flexible as this preprocessor definition? Any help would be greatly appreciated.
[fxfortran]module memory
use iso_c_binding
#define my_allocate(fpntr) call c_f_pointer\
(my_allocate_call (sizeof(fpntr)), fpntr)
implicit none
type :: my_alloc_type
integer*4 :: a
real*8 :: b
real*8, dimension(:), pointer :: c
end type my_alloc_type
type :: my_alloc_type2
logical :: a
real*8 :: b
real*8 :: c(1:20)
end type my_alloc_type2
contains
integer (c_intptr_t) function my_allocate_call (size)
integer (c_size_t) :: size
my_allocate_call = malloc (size)
end function my_allocate_call
subroutine test_my_allocation
type (my_alloc_type), pointer :: t
my_allocate (t)
type (my_alloc_type2), pointer :: t2
my_allocate (t2)
end subroutine test_my_allocation
end module memory [/fxfortran]
Using the preprocessor definition "my_allocate" I can allocate any derived type. I could of course simply use "allocate". For other reasons I want to write my own allocation routines. Now, my problem is that I cannot call the subroutine (i.e. preprocessor definition) "my_allocate" from outside the module. How can I implement a "regular" subroutine which is as flexible as this preprocessor definition? Any help would be greatly appreciated.
Link Copied
3 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Why not just write a small wrapper routine that does what you want? I don't see preprocessor macros as the right tool for this.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thank you for the reply. How would you implement this "small wrapper routine"? The only solution that I could think of was something like this.
[fortran]interface my_allocate module procedure my_allocate_type1 module procedure my_allocate_type2 end interface ... subroutine my_allocate_type1 (fpntr) type (my_alloc_type), pointer :: fpntr integer (c_intptr_t) :: cpntr cpntr = my_allocate_call (sizeof(fpntr)) call c_f_pointer (cpntr, fpntr)
end subroutine my_allocate_type1 subroutine my_allocate_type2 (fpntr) type (my_alloc_type2), pointer :: fpntr integer (c_intptr_t) :: cpntr cpntr = my_allocate_call (sizeof(fpntr)) call c_f_pointer (cpntr, fpntr) end subroutine my_allocate_type2 [/fortran]
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Yes, something like that.

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