Denis
Denis

Reputation: 1534

make sure Fortran common variables are initialized to zero in a third-party library

I am using an old Fortran library from C++. This library has debug.h where a number of common block int variables are defined that are used throughout the library to control terminal output.

The issue is that when I compile this library with gfortran 4.8.5 on a HPC cluster, those common block variables are not zero. Whereas I have no issues with them on macOS or Ubuntu with gfortran 7.2.0.

Since I don't use Fortran at all (I call Fortran functions from C++), the only explanation I can think of is that those variables are not initialized to zero.

Is there a way I can double check they are indeed initialized to zero or force them to always be initialized to zero?

Upvotes: 1

Views: 2536

Answers (1)

Fortran variables are not set to any value by themselves unless you use the DATA statement (inside BLOCK DATA for COMMON blocks) or Fortran 90+ initialization. Their value is undefined until you assign a value to them in your code.

You can use a compiler-specific option of GCC

-finit-real=zero
-finit-integer=0

to set initial values to variables. Better double check it indeed includes COMMON blocks.

See https://gcc.gnu.org/onlinedocs/gfortran/Code-Gen-Options.html

Upvotes: 4

Related Questions