Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

Assinged Pointer Access violation error fortran 90

Aly__Omar
Beginner
606 Views

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
1 Solution
mecej4
Honored Contributor III
606 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
jimdempseyatthecove
Honored Contributor III
606 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
mecej4
Honored Contributor III
607 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.

0 Kudos
Reply