Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
Announcements
Community in read-only mode during platform upgrade. Learn more​>
29657 Discussions

Compiler automatically set not declared variables to zero

Gabriele_B_
Beginner
2,726 Views

Hi,

I am developing an already existing code. When by mistake I insert a not declared variable in the code, for example because of a typo, the compiler don't give me back an error but define this variable by itself and set it to zero. In this way it takes a lot of time to find bugs.

How can I avoid it?

I'm compiling with:

ifort -O3 -axCORE-AVX2 -xSSE4.2 -ipo code.f -o code.exe -extend-source -r8 -lmpi -vec-report2

Thanks

GB

0 Kudos
3 Replies
jimdempseyatthecove
Honored Contributor III
2,726 Views

Add "implicit none"

program YourProgram
  use YourModule ! place modules here
  implicit none
  real :: YourDataHere
  ...

Do the same for subroutines and functions.

You should also make a test run with runtime diagnostics enabled (and compile time interface checking)

Jim Dempsey

0 Kudos
mecej4
Honored Contributor III
2,726 Views

the compiler don't give me back an error but define this variable by itself and set it to zero.

There are millions of lines of code in Fortran that use implicit typing, and it is not an error to use implicit typing. Some compilers may have an option to apply "IMPLICIT NONE" to all routines being compiled.

I can assure you that unless you ask for it, using the /Qzero or similar options, the compiler does not set local variables to zero. However, it may happen that the memory locations assigned to the variable contain zeros. A few minutes later, if you rerun the program, you may see different initial values. It is the programmer's responsibility to initialize variables using one of the available facilities.

 

0 Kudos
FortranFan
Honored Contributor III
2,726 Views

As Jim suggested, it may help, especially in the long run, if you can introduce "implicit none" in your code.

Also, look into compiler warning options as shown here: https://software.intel.com/en-us/node/579575, particularly -warn declarations and unused.

You may also want to look into -init compiler specification and check if your code can handle signaling NaN, etc.: https://software.intel.com/en-us/node/579555

An additional option to consider is runtime checking: see -check uninit at https://software.intel.com/en-us/node/579521.

0 Kudos
Reply