# Bug or undefined behaviour in passing zero-sized arrays as optional argument?

**URL:** <https://fortran-lang.discourse.group/t/bug-or-undefined-behaviour-in-passing-zero-sized-arrays-as-optional-argument/2284>\
**Category:** Help\
**Created:** [November 16, 2021, 9:39am UTC](https://fortran-lang.discourse.group/t/bug-or-undefined-behaviour-in-passing-zero-sized-arrays-as-optional-argument/2284 "2021-11-16T09:39:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![zedthree](https://avatars.discourse-cdn.com/v4/letter/z/f4b2a3/32.png) [@zedthree](https://fortran-lang.discourse.group/u/zedthree)\
**Post date:** [November 16, 2021, 9:39am UTC](https://fortran-lang.discourse.group/t/bug-or-undefined-behaviour-in-passing-zero-sized-arrays-as-optional-argument/2284/1 "2021-11-16T09:39:19Z")

</div>

Here’s a little toy example:

```fortran
  subroutine print_present(opt, name)
    implicit none
    real, intent(in), dimension(:), optional :: opt
    character(len=*), intent(in) :: name
    print*,name, present(opt)
  end subroutine print_present

```

Passing a zero-sized array seems to result in what looks like either undefined behaviour or a bug:

```fortran
call print_present([real::], "Literal zero-sized array")

```

Gfortran mostly says this is not `present`, but sometimes it does evaluate to true. Intel seems to always evaluate to true.

Passing an unallocated array does seem to always evaluate to false, on both gfortran and Intel compilers. Passing an allocatable array that’s zero-sized does seem to evaluate to true for both compilers. It seems to only be zero-sized literal arrays and unallocated arrays that have issues.

Here’s a godbolt reproducer to have a play around with:

> **[Compiler Explorer - Fortran](https://godbolt.org/z/hsPTqa19z)**
>
> program tmp
> implicit none
> real, dimension(:), allocatable :: t
> integer :: i
> allocate(t(1))
> call print\_present(t, "Allocated 1")
> deallocate(t)
> 
> allocate(t(0))
> call print\_present(t, "Allocated 0")
> deallocate(t)
> 
> call...

Does anybody know what the standard says here? This definitely feels like a bug to me.

---

<div class="post-metadata">

**Author:** ![msz59](https://avatars.discourse-cdn.com/v4/letter/m/3d9bf3/32.png) [@msz59](https://fortran-lang.discourse.group/u/msz59)\
**Post date:** [November 16, 2021, 11:38am UTC](https://fortran-lang.discourse.group/t/bug-or-undefined-behaviour-in-passing-zero-sized-arrays-as-optional-argument/2284/2 "2021-11-16T11:38:58Z")

</div>

Looks like a gfortran bug. According to the F2018 standard interpretation document (J3/18-007r1), section 15.5.2.12 it should be “present”, no matter whether zero or non-zero sized.

Interestingly (regarding your snippet on [goldbolt.org](http://goldbolt.org)), the _presence_ of an unallocated array being passed as an optional argument depends on whether the dummy optional arg has `allocatable` attribute. If it does have it, the argument is considered as present, otherwise non-present.

```auto
program main

  real, allocatable :: t(:)
  print *, allocated(t)
  call print_present("Unallocated, allocatable dummy", opt1=t)
  call print_present("Unallocated", opt2=t)
  
  contains

    subroutine print_present(name, opt1, opt2)
      implicit none
      real, intent(in), dimension(:), allocatable, optional :: opt1
      real, intent(in), dimension(:), optional :: opt2
      character(len=*), intent(in) :: name
      print*,name, present(opt1), present(opt2)
    end subroutine print_present

end program main

```

gives

```auto
 F
 Unallocated, allocatable dummy T F
 Unallocated F F

```

consistently both in gfortran and ifort.

---

<div class="post-metadata">

**Author:** ![Beliavsky](https://avatars.discourse-cdn.com/v4/letter/b/ba8739/32.png) [@Beliavsky](https://fortran-lang.discourse.group/u/Beliavsky)\
**Post date:** [November 16, 2021, 11:55am UTC](https://fortran-lang.discourse.group/t/bug-or-undefined-behaviour-in-passing-zero-sized-arrays-as-optional-argument/2284/3 "2021-11-16T11:55:03Z")

</div>

Please see the thread [An unallocated variable passed as an argument is not PRESENT](https://fortran-lang.discourse.group/t/an-unallocated-variable-passed-as-an-argument-is-not-present/1724) .

---

<div class="post-metadata">

**Author:** ![zedthree](https://avatars.discourse-cdn.com/v4/letter/z/f4b2a3/32.png) [@zedthree](https://fortran-lang.discourse.group/u/zedthree)\
**Post date:** [November 16, 2021, 12:11pm UTC](https://fortran-lang.discourse.group/t/bug-or-undefined-behaviour-in-passing-zero-sized-arrays-as-optional-argument/2284/4 "2021-11-16T12:11:22Z")

</div>

Well that is a bit of a pain with the unallocated variables, because it also means type information is not passed along, or you need another overload to make the dummy argument `allocatable`. I’m not sure that that is a good feature.

Looks like the zero-sized literal is definitely a bug though, thanks! I’ll report it to gfortran.
