Passing a null pointer to a subroutine fails with segv

Passing a null pointer to a subroutine fails with segv

!==================================================
!
! Compile and run using:
!  ifort optional-bug.f90
!  ./a.out
!==================================================
program option
  implicit none

  real, target  :: at, dt
  real, pointer :: ap, dp

  at = 10; dt = 40

  ap => at
  dp => dt
  dp => null()              ! Uncommenting this line crashes

  print*, 'before ap, dp', ap, dp

  call mysub(ap, dp)

  print*, 'after ap, dp', ap, dp

  contains

    subroutine mysub(aa, dd)

      real :: aa
      real :: dd

      aa = 1000.*aa
      print*, ' Inside mysub aa = ', aa

    end subroutine mysub

end program option

 

3 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

I think that you are not interpreting the program behavior correctly. It is not the passing of a null pointer (for an argument that is not referenced) that causes the SEGV, but the referencing of the target in the two I/O statements.

Remove ", dp" from the two PRINT statements, and you will see that the program runs with no SEGV happening.

Thank you very much!  We missed that one!

Let me try the original code I started with, which had to do with optional arguments.  Thanks again!

Leave a Comment

Please sign in to add a comment. Not a member? Join today