mecej4
Joined: 31 Oct 2006 Posts: 1692
|
Posted: Sat Mar 26, 2022 12:54 pm Post subject: FTN95 computes wrong size of array |
|
|
The following is a modified version of a very compact code to print out prime integers, by Arjen Markus, author of the book Modern Fortran in Practice, Cambridge, 2012 ( https://www.cambridge.org/core/books/modern-fortran-in-practice/BC5BD23B2E478B4D457C5D6265BA9363 ). The special feature of the program is the use of the PACK intrinsic to do most of the work of sifting through candidate prime numbers. The program works correctly with NAG, Intel and Gfortran, and uses the "allocate on assignment" feature of Fortran 200X.
FTN95 calculates the wrong size (2490 instead of 563) for the array multiples. As a consequence, the (over)allocated array has nearly 2000 elements undefined, which leads to program errors and aborts in the subsequent portions of the program.
Code: | program prime_sieve
implicit none
integer, parameter :: N = 1000, rtN = 31
integer, parameter :: candidates(N/2) = [2,(i, i=3,N-1,2)]
integer, allocatable :: multiples(:)
integer, allocatable :: primes(:)
integer :: i, j
multiples = [(( i*j, i=j,N/j,2), j=3,rtN,2)] ! FTN95 signals Ref. to Undef. Var.
print *,'size(multiples array) = ',size(multiples) ! expected: 563
primes = pack( candidates, [(all(candidates(i) /= multiples), &
i = 1,size(candidates))] )
print '(A,I5,A,I5)','Found',size(primes),' primes <= ',N
end program prime_sieve
|
|
|