In C#, if you want to indicate that an integer may or may not be there, you can mark it nullable, like this:
int? x = null;
This is useful when you don’t have a value that’s “invalid”, like 0, that lets you know the value isn’t there. In Fortran, marking a scalar allocatable achieves a similar result
integer, allocatable :: x
I know you can’t rely on local variables to have any value when declared. Is the same true for allocatable variables? My actual use-case involves an array of custom types:
type :: my_type
real, allocatable :: ent
real, allocatable :: lvg
end type
subroutine s(n)
integer, intent(in) :: n
type(my_type), allocatable :: arr(:)
allocate(arr(n))
! ...
end subroutine
Can I count on all elements of arr to have unallocated ent and lvg variables?
You may find gfortran’s behavior surprising in this regard. You can bypass the surprise (allocated status upon second call) by explicitly deallocating the local allocatable objects before exiting the procedure.
I’ve come across the following question on StackOverflow, which may be related to the unexpected behavior of allocatable by Gfortran (linked in the above post, which is still reproducible with Gfortran-14 on Godbolt). Indeed, this may be a bug of the option -fmax-stack-var-size itself, though not sure about details.