Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

unitialized variable array behaviour in fortran

Status
Not open for further replies.

daveskunk

Programmer
Jul 13, 2008
2
DE
Greetings all,

I have some code in which there was a variable array declared but not intialized. I realised this after seeing some strange results.
I've characterized the problem, but I would like to know why it is the way it is. Here is an example program:

implicit none
integer testvar(nmax), j
do j = 1, nmax
write(*,*) j, " ", testvar(j)
end do
end

where nmax is actually some integer.
When nmax is small then the values of testvar are typically garbage, e.g. with nmax=5:
1 1073779242
2 134513212
3 -1077337612
4 1073854404
5 1

However, when nmax increases beyond a critical value the numbers of the first few elements begin to be 0; for example, with nmax=628:
1 0
2 1073743302
etc.

and beyond nmax=628 the number of elements at the beginning of the array that have zero value increases linearly with nmax. i.e.

nmax nelements=0_at_beginning_of_array
628 1
629 2
630 3
631 4
etc.
800 173
etc.

and the other array elements have garbage values (between high positive and high negative).
My main query is: why do elements at the beginning of the arrays get initialized with 0 when the array is beyond a certain size?

Additionally, the pattern of garbage actually seems quite uniform. That is, the distribution of postive, negative and zero values moves along the array staying roughly constant - why is that? If you could explain this supplementary query that would be a bonus, but I am very eager to find the answer to the former question.
I have tested this with fortran 77 and fortran 95 compilers on both debian (Ubuntu) and Windows environments (using cygwin).

I would really appreciate it if someone could answer this for me.

Cheers,

David
 
It may be specific to your machine - if you run it on another machine, you might get different results.

Are you always running things in the same order? If you are, then the program may be always grabbing the same chunk of memory so it will always be initialized the same way. Try running a different program in between like ls or play a game after compilation.
 
Hi xwb, thanks for your response. I've ran the program on a couple of laptops (one running Ubuntu, another running Windows) and a desktop running another flavour of Linux.
It is a good point you make about accessing the same part of memory. The code I provided, I actually run that multiple times with different values of 'nmax'. The execution is handled by a c-shell script. I have ran multiple times with nmax=1-->2000 in order, subsets around the point of interest in various orders, after rebooting the machine, etc. The behaviour is machine/environment dependent but the general pattern is the same and the behaviour on any one machine/environment is identical despite reboots, running other programs etc.
Cheers,
David
 
Fortran Standard said:
If a variable, array element, or substring is
undefined, it does not have a predictable value.
What's an interesting to discover unpredictable values, undefined behaviour and the other world tricks?

Get a REAL array, try to print undefined elements - you have a chance to catch floating point exception (NaNs etc)...
Why?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top