- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hello Everyone,
I have written a simple program to create pointer as global variables and assigned it in another module, then it visable and accessible in this module but in another module it gives access violation.
example:
module global_vars
use data_structure
implicit none
integer(8) :: x_dim
type(ds), dimension(:), pointer :: x_ptr
end module global_vars
module initialize
use data_structure
use global_vars
subroutine load_data()
use global_vars, only : x_ptr, x_dim
integer(8) :: var1, var2
type(ds) , allocatable, dimension(:), target :: trgt
open(3, file='Input.txt')
read (3,*), var1
var2 = 2
x_dim = var1 * var2
allocate(trgt(x_dim))
x_ptr => trgt
!here I can access the pointer
print*, x_ptr(1)
end subroutine load_data
end module initialize
module operation
use global_vars
subroutine operate()
use global_vars, only : x_ptr
!here I can't access the pointer
print*, x_ptr(1) ! access violation happens
end subroutine operate
end module opeartion
in the main program
program main_prg
use intialize
use operation
!I call load_data
!I call operate
end program main_prg
Any help or clue on this ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
As Jim Dempsey noted, the code posted is not complete, but here is something to check. The array trgt(:) is local to subroutine load_data(), and does not have the SAVE attribute;therefore, it gets deallocated when that subroutine is exited. After that, any pointer associated with trgt is "dangling" and should not be dereferenced.
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Can you post a complete example?
Note, your pasted code is incorrect in that the subroutines within the modules are not following a "contains" statement. As-Is they will compile with errors.
Also use the {...}code button to past sample code.
Jim Dempsey
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
As Jim Dempsey noted, the code posted is not complete, but here is something to check. The array trgt(:) is local to subroutine load_data(), and does not have the SAVE attribute;therefore, it gets deallocated when that subroutine is exited. After that, any pointer associated with trgt is "dangling" and should not be dereferenced.

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