Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fortran unassigned array

I just begin learning Fortran, and I come across this issue. Consider the following simple code.

PROGRAM random

INTEGER, DIMENSION(12):: array
PRINT *, array

END PROGRAM random

The array is not assigned value, but can be printed, and it seems to have a few random elements and several zero elements. However, if I consider a shorter array, say I declare

INTEGER, DIMENSION(5):: array

then the printed array has all elements = 0. I wonder what is happening here?

like image 619
Lelouch Avatar asked Jan 10 '23 03:01

Lelouch


1 Answers

When you define an array and try to look at the values it contains (i.e. by printing) before initialising it, the behaviour is undefined. It depends on compiler to compiler. While one compiler may automatically set all the values to zero (which many of us think that to be the default), another compiler may set it to completely random values. Which is why you are seeing the array values sometimes zero and sometimes not.

However, many of the compilers have options to initialise an unassigned array to zeros at compiler level. It is always advised that one should initialise an array always before using it!

like image 144
Madhurjya Avatar answered Mar 25 '23 07:03

Madhurjya