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
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