# Gfortran uninitialized warnings

**URL:** <https://fortran-lang.discourse.group/t/gfortran-uninitialized-warnings/3838>\
**Category:** Uncategorized\
**Created:** [June 26, 2022, 10:31am UTC](https://fortran-lang.discourse.group/t/gfortran-uninitialized-warnings/3838 "2022-06-26T10:31:50Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![gnikit](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/gnikit/32/1213_2.png) [@gnikit](https://fortran-lang.discourse.group/u/gnikit)\
**Post date:** [June 26, 2022, 10:31am UTC](https://fortran-lang.discourse.group/t/gfortran-uninitialized-warnings/3838/1 "2022-06-26T10:31:50Z")

</div>

I have been running into these `-Wuninitialized` warnings in `gfortran - 12.1.0` (but also earlier versions going back to at least 9.4.0) and I was wondering if they are false positives.

```fortran
program main
    implicit none
    character(len=1), allocatable :: a(:)
    a = ["a"]
    print*, a
end program main

```

### Output

```auto
test.f90:4:13:

    4 | a = ["a"]
      | ^
Warning: ‘a.offset’ is used uninitialized [-Wuninitialized]
test.f90:3:41:

    3 | character(len=1), allocatable :: a(:)
      | ^
note: ‘a’ declared here
test.f90:4:13:

    4 | a = ["a"]
      | ^
Warning: ‘a.dim[0].lbound’ is used uninitialized [-Wuninitialized]
test.f90:3:41:

    3 | character(len=1), allocatable :: a(:)
      | ^
note: ‘a’ declared here
test.f90:4:13:

    4 | a = ["a"]
      | ^
Warning: ‘a.dim[0].ubound’ is used uninitialized [-Wuninitialized]
test.f90:3:41:

    3 | character(len=1), allocatable :: a(:)
      | ^
note: ‘a’ declared here
test.f90:4:13:

    4 | a = ["a"]
      | ^
Warning: ‘a.dim[0].lbound’ may be used uninitialized [-Wmaybe-uninitialized]
test.f90:3:41:

    3 | character(len=1), allocatable :: a(:)
      | ^
note: ‘a’ declared here
test.f90:4:13:

    4 | a = ["a"]
      | ^
Warning: ‘a.dim[0].ubound’ may be used uninitialized [-Wmaybe-uninitialized]
test.f90:3:41:

    3 | character(len=1), allocatable :: a(:)
      | ^
note: ‘a’ declared here
test.f90:4:13:

    4 | a = ["a"]
      | ^
Warning: ‘a.dim[0].ubound’ may be used uninitialized [-Wmaybe-uninitialized]
test.f90:3:41:

    3 | character(len=1), allocatable :: a(:)
      | ^
note: ‘a’ declared here
test.f90:4:13:

    4 | a = ["a"]
      | ^
Warning: ‘a.dim[0].lbound’ may be used uninitialized [-Wmaybe-uninitialized]
test.f90:3:41:

    3 | character(len=1), allocatable :: a(:)
      | ^
note: ‘a’ declared here

```

Any ideas what is going on? Is that the expected behaviour and I am missing something?

---

<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:** [June 26, 2022, 10:59am UTC](https://fortran-lang.discourse.group/t/gfortran-uninitialized-warnings/3838/2 "2022-06-26T10:59:39Z")

</div>

Yes, the `-Wuninitialized` option of gfortran produces so many false positives that it should usually be turned off, especially if you use the allocation-on-assignment feature of Fortran. For example, for the code

```auto
implicit none
integer, allocatable :: v(:)
v = [10,20]
print*,v
end

```

you get

```auto
c:\fortran\test>gfortran -Wall -Wextra xgfortran_warn.f90
xgfortran_warn.f90:3:11:

    3 | v = [10,20]
      | ^
Warning: 'v.offset' is used uninitialized [-Wuninitialized]
xgfortran_warn.f90:2:28:

    2 | integer, allocatable :: v(:)
      | ^
note: 'v' declared here
xgfortran_warn.f90:3:11:

    3 | v = [10,20]
      | ^
Warning: 'v.dim[0].lbound' is used uninitialized [-Wuninitialized]
xgfortran_warn.f90:2:28:

    2 | integer, allocatable :: v(:)
      | ^
note: 'v' declared here
xgfortran_warn.f90:3:11:

    3 | v = [10,20]
      | ^
Warning: 'v.dim[0].ubound' is used uninitialized [-Wuninitialized]
xgfortran_warn.f90:2:28:

    2 | integer, allocatable :: v(:)
      | ^
note: 'v' declared here
xgfortran_warn.f90:3:11:

    3 | v = [10,20]
      | ^
Warning: 'v.dim[0].lbound' may be used uninitialized [-Wmaybe-uninitialized]
xgfortran_warn.f90:2:28:

    2 | integer, allocatable :: v(:)
      | ^
note: 'v' declared here
xgfortran_warn.f90:3:11:

    3 | v = [10,20]
      | ^
Warning: 'v.dim[0].ubound' may be used uninitialized [-Wmaybe-uninitialized]
xgfortran_warn.f90:2:28:

    2 | integer, allocatable :: v(:)
      | ^
note: 'v' declared here
xgfortran_warn.f90:3:11:

    3 | v = [10,20]
      | ^
Warning: 'v.dim[0].ubound' may be used uninitialized [-Wmaybe-uninitialized]
xgfortran_warn.f90:2:28:

    2 | integer, allocatable :: v(:)
      | ^
note: 'v' declared here
xgfortran_warn.f90:3:11:

    3 | v = [10,20]
      | ^
Warning: 'v.dim[0].lbound' may be used uninitialized [-Wmaybe-uninitialized]
xgfortran_warn.f90:2:28:

    2 | integer, allocatable :: v(:)
      | ^
note: 'v' declared here

```

for gfortran 12.0.1 20220213.

`gfortran -Wall -Wextra -Wno-uninitialized xgfortran_warn.f90`

is appropriately silent.

---

<div class="post-metadata">

**Author:** ![gnikit](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/gnikit/32/1213_2.png) [@gnikit](https://fortran-lang.discourse.group/u/gnikit)\
**Post date:** [June 26, 2022, 11:04am UTC](https://fortran-lang.discourse.group/t/gfortran-uninitialized-warnings/3838/3 "2022-06-26T11:04:47Z")

</div>

I see, thanks @Beliavsky. I am curious if there is a bug report tracking this in Bugzilla; I couldn’t find anything, so maybe it would be a good idea to file one. I find the the uninitialised warnings in C++ extremely useful, so it would be great if gfortran could also improve.

---

<div class="post-metadata">

**Author:** ![gnikit](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/gnikit/32/1213_2.png) [@gnikit](https://fortran-lang.discourse.group/u/gnikit)\
**Post date:** [June 26, 2022, 11:20am UTC](https://fortran-lang.discourse.group/t/gfortran-uninitialized-warnings/3838/4 "2022-06-26T11:20:15Z")

</div>

I did a more thorough search for bug reports in GCC and I found some existing bug reports for gfortran, namely:

- [56670 – Allocatable-length character var causes bogus warning with -Wuninitialized](https://gcc.gnu.org/bugzilla/show_bug.cgi?id=56670)
- [91442 – Wrong "may be used uninitialized" warning with allocation on assignment](https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91442)
- [77504 – [10/11/12/13 Regression] "is used uninitialized" with allocatable string and array constructors](https://gcc.gnu.org/bugzilla/show_bug.cgi?id=77504)

Full [Bug List](https://gcc.gnu.org/bugzilla/buglist.cgi?bug_status=UNCONFIRMED&bug_status=NEW&bug_status=ASSIGNED&bug_status=SUSPENDED&bug_status=WAITING&bug_status=REOPENED&cf_known_to_fail_type=allwords&cf_known_to_work_type=allwords&component=fortran&list_id=350228&query_format=advanced&short_desc=uninitialized&short_desc_type=allwordssubstr)

---

<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:** [June 26, 2022, 11:27am UTC](https://fortran-lang.discourse.group/t/gfortran-uninitialized-warnings/3838/5 "2022-06-26T11:27:20Z")

</div>

I posted another bug report before seeing your latest post:

[https://gcc.gnu.org/bugzilla/show\_bug.cgi?id=106089](https://gcc.gnu.org/bugzilla/show_bug.cgi?id=106089)

---

<div class="post-metadata">

**Author:** ![gnikit](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/gnikit/32/1213_2.png) [@gnikit](https://fortran-lang.discourse.group/u/gnikit)\
**Post date:** [June 26, 2022, 5:18pm UTC](https://fortran-lang.discourse.group/t/gfortran-uninitialized-warnings/3838/7 "2022-06-26T17:18:40Z")

</div>

Thanks @kargl for coming to the rescue, I appreciate it!

---

<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:** [June 26, 2022, 6:59pm UTC](https://fortran-lang.discourse.group/t/gfortran-uninitialized-warnings/3838/9 "2022-06-26T18:59:50Z")

</div>

OK, but that would make the reader of the code wonder why the `allocate(a(0))` statement appears. I would rather use the `-Wno-uninitialized` option than add such code.

ETA: `gfortran -Wuninitialized` is silent about uninitialized variable `abc` in the code below, which further suggests disabling the option.

```auto
implicit none
integer, allocatable :: v(:), abc(:)
character(len=1), allocatable :: a(:)
a = ["a"]
print*, a
v = [10,20]
print*,v
allocate (abc(1))
print*,abc
end

```
