# Some type of automatic initialization of variables?

**URL:** https://fortran-lang.discourse.group/t/some-type-of-automatic-initialization-of-variables/4115
**Category:** Uncategorized
**Created:** [August 3, 2022, 3:42pm UTC](https://fortran-lang.discourse.group/t/some-type-of-automatic-initialization-of-variables/4115 "2022-08-03T15:42:34Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![giraffe](https://avatars.discourse-cdn.com/v4/letter/g/f1d935/32.png) [@giraffe](https://fortran-lang.discourse.group/u/giraffe)
#### Post date: [August 3, 2022, 3:42pm UTC](https://fortran-lang.discourse.group/t/some-type-of-automatic-initialization-of-variables/4115/1 "2022-08-03T15:42:34Z")

</div>

I’m porting a 32 bit Fortran program (on a CENTOS 7 computer) to a 64 bit (RHEL 7) computer. I’ve noticed variables that were automatically initialized to -0.000000 on the 32 bit machine are -nan on the 64 bit machine.

These variables aren’t being initialized by statements in the program.

My guess is these variables probably are automatically being set by the gfortran compiler on the 32 bit machine.

Question: What practical things can I do about this?

---

<div class="post-metadata">

### Author: ![lmiq](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/lmiq/32/555_2.png) [@lmiq](https://fortran-lang.discourse.group/u/lmiq)
#### Post date: [August 3, 2022, 4:30pm UTC](https://fortran-lang.discourse.group/t/some-type-of-automatic-initialization-of-variables/4115/2 "2022-08-03T16:30:13Z")

</div>

You should not rely on “automatic” initialization of variables, initialize them explicitly to zero if needed.

---

<div class="post-metadata">

### Author: ![RonShepard](https://avatars.discourse-cdn.com/v4/letter/r/a3d4f5/32.png) [@RonShepard](https://fortran-lang.discourse.group/u/RonShepard)
#### Post date: [August 3, 2022, 4:30pm UTC](https://fortran-lang.discourse.group/t/some-type-of-automatic-initialization-of-variables/4115/3 "2022-08-03T16:30:28Z")

</div>

You are fortunate that the uninitialized numbers are NaN because this allows you to identify them and fix the fortran source code. When uninitialized variables have random bits, they are much harder to locate and correct. Some compilers have options to set unitialized values to specific values. NaN is a good choice for floating point variables. Integers can be set to large positive or negative values, which makes it easier to locate when they are used, for example, as an array index. Some compilers have options to flag unitialized entities when they are first used.

---

<div class="post-metadata">

### Author: ![rwmsu](https://avatars.discourse-cdn.com/v4/letter/r/48db29/32.png) [@rwmsu](https://fortran-lang.discourse.group/u/rwmsu)
#### Post date: [August 3, 2022, 4:47pm UTC](https://fortran-lang.discourse.group/t/some-type-of-automatic-initialization-of-variables/4115/4 "2022-08-03T16:47:50Z")

</div>

No other way to say this but relying on the compiler/OS to initialize anything by default (unless you are using a compiler option to do it) is shoddy programming.The results can be compiler dependent.

Try this program on your systems.

```auto
Program realz
  
  USE ISO_FORTRAN_ENV

  Implicit NONE

  Real(REAL32) :: z32
  Real(REAL64) :: z64

  Print *,''
  Print *,' uninitialized'

  Print *,' z32 = ? - ', z32
  Print *,' z64 = ? - ', z64

  z32 = -0.000000
  z64 = -0.000000

  Print *,''
  Print *,' z32 = -0.000000 - ', z32
  Print *,' z64 = -0.000000 - ', z64

  z32 = -0.000000_REAL32
  z64 = -0.000000_REAL64

  Print *,''
  Print *,' z32 = -0.000000_REAL32 - ', z32
  Print *,' z64 = -0.000000_REAL64 - ', z64

  z32 = REAL(-0,REAL32)
  z64 = REAL(-0,REAL64)

  Print *,''
  Print *,' z32 = REAL(-0,REAL32) - ', z32
  Print *,' z64 = REAL(-0,REAL64) - ', z64
  Print *,''
  Print *,''

  STOP

End Program realz

```

I get the following results for ifort, gfortran-11, and nvfortran on a Linux Mint (Ubuntu 18.04 LTS) 64 bit system. Only ifort appears to initialize to 0.0 by default but that just might be an optimization thing so don’t assume ifort does this for all unitialized values.

```auto
ifort OneAPI 2021.6.0

  uninitialized
  z32 = ? - 0.0000000E+00
  z64 = ? - 0.000000000000000E+000
 
  z32 = -0.000000 - 0.0000000E+00
  z64 = -0.000000 - 0.000000000000000E+000
 
  z32 = -0.000000_REAL32 - 0.0000000E+00
  z64 = -0.000000_REAL64 - 0.000000000000000E+000
 
  z32 = REAL(-0,REAL32) - 0.0000000E+00
  z64 = REAL(-0,REAL64) - 0.000000000000000E+000

gfortran-11

  uninitialized
  z32 = ? - 6.41672678E+27
  z64 = ? - 0.0000000000000000     
 
  z32 = -0.000000 - -0.00000000    
  z64 = -0.000000 - -0.0000000000000000     
 
  z32 = -0.000000_REAL32 - -0.00000000    
  z64 = -0.000000_REAL64 - -0.0000000000000000     
 
  z32 = REAL(-0,REAL32) - 0.00000000    
  z64 = REAL(-0,REAL64) - 0.000000000000000

nvfortran 22-2-0

  uninitialized
  z32 = ? - 4.5916347E-41
  z64 = ? - 1.1520304496612931E-310
 
  z32 = -0.000000 - -0.000000    
  z64 = -0.000000 - -0.000000000000000     
 
  z32 = -0.000000_REAL32 - -0.000000    
  z64 = -0.000000_REAL64 - -0.000000000000000     
 
  z32 = REAL(-0,REAL32) - 0.000000    
  z64 = REAL(-0,REAL64) - 0.000000000000000

```

---

<div class="post-metadata">

### Author: ![RonShepard](https://avatars.discourse-cdn.com/v4/letter/r/a3d4f5/32.png) [@RonShepard](https://fortran-lang.discourse.group/u/RonShepard)
#### Post date: [August 3, 2022, 7:46pm UTC](https://fortran-lang.discourse.group/t/some-type-of-automatic-initialization-of-variables/4115/6 "2022-08-03T19:46:53Z")

</div>

You can define variables in fortran in the declaration statement or in an assignment statement. If you do the former, then you should add the SAVE attribute to make clear what is happening. Which of those choices is appropriate depends on how the variables are used, sometimes one is appropriate and sometimes the other. Of course, all variables should be defined to appropriate values before they are used. That is why the original NaN values are so useful, they help the programmer identify the mistakes in the fortran code. A good time to fix the code is when it is still running “correctly” on the old compiler and OS. Then you can print out intermediate results to help locate where uninitialized variables are being referenced. If you only have the buggy code, and you can only compile it on a machine that does not allow any command-line default initializations, then it is much harder to debug the code.

---

<div class="post-metadata">

### Author: ![DavidB](https://avatars.discourse-cdn.com/v4/letter/d/76d3ee/32.png) [@DavidB](https://fortran-lang.discourse.group/u/DavidB)
#### Post date: [August 4, 2022, 3:06am UTC](https://fortran-lang.discourse.group/t/some-type-of-automatic-initialization-of-variables/4115/7 "2022-08-04T03:06:05Z")

</div>

[valgrind](https://valgrind.org/) will detect some uninitialized variables. It probably won’t detect variables initialized by the compiler to default values.
