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

Puzzling behavior of compiler

New Contributor I

In this simple example, ipr1 never gets set to the right value.

   program prog1
   integer(8) ipr
2 read *,ipr
   call sub1(ipr)
   go to 2
  end program
  subroutine sub1(ipr)
  integer(8) ipr,ipr1,ipr2
  ipr1=ipr2          ! never gets executed
  print *,"ipr1,2=",ipr1,ipr2


However, the problem goes away when I use a DATA statement to initialize ipr2. 

integer(8) ipr,ipr1,ipr2/0/


Is this something in the compiler ?

0 Kudos
1 Reply
Black Belt

IPR2 in the subroutine is a local variable. By default, it is not saved, and it becomes undefined whenever the subroutine is entered. The program is invalid because it uses the undefined value. For the same reason, in this case there is no "right value".

Adding a DATA statement to the subroutine gives the variable IPR2 the SAVE attribute. The program now is, as a result, predictable and standard conforming.

The compiler will help you catch this bug at run time if you use the /check option.

The comment "never gets executed" is not correct. You can step through the program using the debugger to verify that, after compiling with /debug or /Zi.

0 Kudos