gFortran allocatable arrays and intrinsic all function

Be carefull when using allocatable arrays and all builtin method. The compiler is not able to detect the different sizes. So it provides a false positive when tested with the first value  of the array.

Check below an example:

 program allbug  
  implicit none  
  real, allocatable :: values(:,:)  
  allocate(values(3,1))  
  values(:, 1) = [26., 27., 28.]  
  write(*,*) "values= ", values  
  write(*,*) "the same vs 26?", all(values(:, 1) == [26.] )  
  write(*,*) "the same vs 27?", all(values(:, 1) == [27.] )  
 end program  

 

 The output:

 bash:~/projects$ ./a.out  
  values=  26.0000000    27.0000000    28.0000000    
  the same vs 26? T  <---- HERE!  
  the same vs 27? F   

On the other hand, when it is done with an explicit array an error is raised at compilation time.

 all-fixed.f90:10:36-51:  
   10 |  write(*,*) "the same vs 26?", all(values(:, 1) == [26.] )  
    |                  1       2  
 Error: Shapes for operands at (1) and (2) are not conformable  
 all-fixed.f90:11:36-51:  
   11 |  write(*,*) "the same vs 26?", all(values(:, 1) == [27.] )  
    |                  1       2  
 Error: Shapes for operands at (1) and (2) are not conformable  

gfortran: 9.3

Comentaris

Entrades populars d'aquest blog

L'Europa del S. X al S.XIX

Array of pointers in Fortran

Renda fixa