- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I'm trying to pass an allocatable array, which is already allocated, to a subroutine. something like this:
real(8), dimension(:,:), allocatable :: A allocate (A (6,6)) call subroutine mysub(A) subroutine mysub(B) real (8), dimension (:,:) :: B write (*,*) B(1,1)
This causes segmentation fault when mysub is accessing B. I have had the same approach to pass the array to a function and it worked fine. I wonder what's going wrong here.
Thanks,
Alireza
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
If you call a subroutine whose arguments include allocatable or assumed-shape arrays, the interface to the subroutine must be provided in the caller. This can be done in a number of ways, and I suggest that you either create and use a module for this purpose or make the subroutine and its caller contained subroutines.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
You need to provide an explicit interface for subroutine B. Otherwise, the caller doesn't know it is supposed to provide all the array bounds information etc., as opposed to just a starting address. You can look up "Doctor Fortran" articles about this, for example, https://software.intel.com/en-us/blogs/2012/01/05/doctor-fortran-gets-explicit-again .
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thanks very much. Now I understand why the function worked, but not the subroutine. The function was defined in a module.
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page