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

Passing a null pointer to a subroutine fails with segv

Raghu_Reddy
Beginner
411 Views
!==================================================
!
! 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

 

0 Kudos
2 Replies
mecej4
Honored Contributor III
411 Views

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.

0 Kudos
Raghu_Reddy
Beginner
411 Views

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!

0 Kudos
Reply