Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Beginner
81 Views

Assinged Pointer Access violation error fortran 90

Jump to solution

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 ?

0 Kudos

Accepted Solutions
Highlighted
Black Belt
81 Views

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.

View solution in original post

0 Kudos
2 Replies
Highlighted
81 Views

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

0 Kudos
Highlighted
Black Belt
82 Views

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.

View solution in original post

0 Kudos