error about default logical variable value with pgi compiler

i have encountered an error about logical variable value assigned by pgi compiler.

the following is my code for test:
module kinds
implicit none
integer,parameter :: &
log_kind=kind(.true.)
end module kinds

program testlog
use kinds
implicit none
logical(log_kind),allocatable :: doqp2(:)
integer i,j,k,m
m=5
allocate(doqp2(m))
call test_log(m,doqp2)
print*,doqp2(1:m)
print*,doqp2(1:10)
deallocate(doqp2)
end

subroutine test_log(m,doqp2)
use kinds
implicit none
logical(log_kind) :: doqp2(m)
integer i,j,k,m
do i=1,m
doqp2(i) = .true.
enddo
end subroutine test_log

after compling it , i used the pgi version 11.9 and i have try the 10.6 too, them give the result is :
T T T T T
T T T T T F F F F T
the last logical value is not F but T, but i compiled it with ifort , which give the another result as follows:
T T T T T
T T T T T T T T T F
the last five logical value in the second line show contrary result.

then i compiled it with other compiler , the result is as following:
with gfortran:
T T T T T
T T T T T T F F F F

with f95:
T T T T T
T T T T T T F F F F

it seems that gfortran and f95 can give the correct result, just because the system deem the those unassigned logical value is FALSE, but in the above result compiled with pgf90 are not all FALSE.


would you like give me some advices?

thanks.

Hi novelbean,

The upper bound of the array is 5, but you’re printing out 10 elements. Hence, anything after the 5th element will contain junk values. To fix, you need to either increase the size of your array ( M >= 10) or not print beyond the end of the array.

Hope this helps,
Mat

thank for your answer.
i have thought those logical variables value unassigned beforehand the fortran system would assign them FALSE value.

Hi novelbean,

You can never assume that memory is initialised and should always initialize the values yourself. There are cases where memory is initilzed to zero (like with a SAVE statement) but this is not standard and may vary by platform and compiler.

However, in this case you do initialize your array, it’s just that you are also accessing memory beyond the end of the array. While here the only consequence is printing junk values, in other cases you can cause your program to crash with a segmentation violation (i.e. accessing protected memory) or get wrong answers.

Reading and writing uninitialised memory (aka a UMR) and out-of-bounds errors are two of the most frequent sources of program error.

Hope this helps,
Mat