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

default initializing variables (heap and stack) to 0

ifortuser
Beginner
850 Views

Hi, I am looking for a intel fortran compiler flag that would allow me to default initialize variables to 0 (or other values), these variables could be declared on the stack, the heap or allocated dynamically.  I didn't find anything myself from the compiler options.

 

program defaultinit

    real :: x

    real(:), allocatable :: y

    allocate(y(10))

end program

With the default initialization, I am hoping I could initialize x and y to 0 during run time without manually initializing them. Is this possible through compiler flags? 

Thanks,

0 Kudos
9 Replies
jimdempseyatthecove
Black Belt
845 Views

Compiler option /Qzero

implicit initialization to zero of local scalar variables of intrinsic type INTEGER, REAL, COMPLEX, or LOGICAL that are saved and not initialized

 

Compiler option /Qinit:<keyword>

   enable/disable(DEFAULT) implicit initialization of local
   variables of intrinsic type INTEGER, REAL, COMPLEX, or
   LOGICAL that are saved and not initialized

The <keyword> specifies the initial value

   keywords: zero (same as /Qzero),
   snan (valid only for floating point variables),
   infinity, minus_infinity (valid only for floating point)
   tiny, minus_tiny (valid only for floating point)
   huge, minus_huge
   arrays

 

For allocatables use SOURCE with scalar

 

allocate(Y(10), SOURCE=0.0) ! or whatever value you want

 

Jim Dempsey

0 Kudos
Steve_Lionel
Black Belt
836 Views

Even better - don't do this, fix your code instead.

0 Kudos
ifortuser
Beginner
832 Views

Thanks, this is to help to identify the bug in the code and fix the problem. Occasionally we have legacy code where someone forgot to default initialize variables and it's difficult to pinpoint where the problem is. Being able to default initialize variables to 0 or infinity will help us to figure out where the problem is.

 

Jim, the -init option looks like exactly what I need. 

0 Kudos
jimdempseyatthecove
Black Belt
830 Views

>>Being able to default initialize variables to 0 or infinity will help us to figure out where the problem is.

Also consider using /Qtrapuv   trap uninitialized variables

And /RTCu report use of variable that was not initialized

And /check:uninit

And /check:bounds

 

Jim Dempsey

 

0 Kudos
jimdempseyatthecove
Black Belt
830 Views

Relying on default initialization will get you into trouble, either sooner or later. Explicitly perform your initialization. 

Implicit initialization is .NOT. part of the Fortran standard.

And why would you presume 0/0.0 is the proper value for initialization?

Jim Dempsey

0 Kudos
Steve_Lionel
Black Belt
824 Views

Don't bother with /Qtrapuv - it doesn't do what it says in the manual. /check:uninit is what you should be using.

0 Kudos
ifortuser
Beginner
819 Views

Great, thanks for all the input. We are not trying to sweep something under the rug, this will help us to debug  problems caused by uninitialized value.

0 Kudos
jimdempseyatthecove
Black Belt
799 Views

While you are at it, add

  /gen-interfaces /warn:all

 

Note, warnings may be errors (in particular interface warnings).

Consider each warning before you dismiss it.

 

Jim Dempsey

 

0 Kudos
Steve_Lionel
Black Belt
794 Views

You don't need /gen-interface anymore if /warn:interfaces is enabled (or /warn:all).

I also like to do a build with /stand (standards checking) to identify extensions I hadn't intended to use - these can sometimes lead to unanticipated results.

0 Kudos
Reply