Potential errors can come up when using allocatable arrays when returned in functions. Given the example found below, an error is created on purpose to demonstrate the compiled cannot detect it.
program allocarr
implicit none
integer, allocatable :: values(:)
allocate(values(6))
values = -1
values(2:4) = calc_something(25) ! the outcome for 3 values
write(*,*) "values=", values
contains
function calc_something(value) result (myarray)
integer, intent(in) :: value
integer, allocatable :: myarray(:)
allocate(myarray(2)) ! the outcome for 2 values only, there is an error
myarray = -2 ! def values
myarray = value
end function calc_something
end program allocarr
Output:
[mypc]$ gfortran allocarr.f90 && ./a.out
values= -1 25 25 0 -1 -1
Bear in mind, potential errors could come from allocatable output arrays.
Valgrind would complain as such:
==2208216== Memcheck, a memory error detector
==2208216== Copyright (C) 2002-2022, and GNU GPL'd, by Julian Seward et al.
==2208216== Using Valgrind-3.21.0 and LibVEX; rerun with -h for copyright info
==2208216== Command: ./a.out
==2208216== Parent PID: 1999926
==2208216==
==2208216== Invalid read of size 4
==2208216== at 0x400C82: MAIN__ (in /home/b/b301060/tests/alloc_array_function/a.out)
==2208216== by 0x400D5A: main (in /home/b/b301060/tests/alloc_array_function/a.out)
==2208216== Address 0x5ea1b28 is 0 bytes after a block of size 8 alloc'd
==2208216== at 0x4C38185: malloc (vg_replace_malloc.c:431)
==2208216== by 0x4009DA: calc_something.0 (in /home/b/b301060/tests/alloc_array_function/a.out)
==2208216== by 0x400C68: MAIN__ (in /home/b/b301060/tests/alloc_array_function/a.out)
==2208216== by 0x400D5A: main (in /home/b/b301060/tests/alloc_array_function/a.out)
==2208216==
==2208216==
==2208216== HEAP SUMMARY:
==2208216== in use at exit: 24 bytes in 1 blocks
==2208216== total heap usage: 23 allocs, 22 frees, 13,616 bytes allocated
==2208216==
==2208216== 24 bytes in 1 blocks are definitely lost in loss record 1 of 1
==2208216== at 0x4C38185: malloc (vg_replace_malloc.c:431)
==2208216== by 0x400B5D: MAIN__ (in /home/b/b301060/tests/alloc_array_function/a.out)
==2208216== by 0x400D5A: main (in /home/b/b301060/tests/alloc_array_function/a.out)
==2208216==
==2208216== LEAK SUMMARY:
==2208216== definitely lost: 24 bytes in 1 blocks
==2208216== indirectly lost: 0 bytes in 0 blocks
==2208216== possibly lost: 0 bytes in 0 blocks
==2208216== still reachable: 0 bytes in 0 blocks
==2208216== suppressed: 0 bytes in 0 blocks
==2208216==
==2208216== For lists of detected and suppressed errors, rerun with: -s
==2208216== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)
Be careful when using allocatable arrays.