5

A simple code:

program main
integer, allocatable :: A(:,:)
integer              :: B(3,4)
B=1
A = B  !A will get allocated, with the same shape and bounds as B
end program main

Compiling the above code with: gfortran-8 -std=f2008 -fcheck=all -Wall -Wextra -fbounds-check -fimplicit-none array.f90

I got the following warning:

Warning: ‘a.offset’ may be used uninitialized in this function Warning: ‘a.dim[0].lbound’ may be used uninitialized in this function Warning: ‘a.dim[0].ubound’ may be used uninitialized in this function [-Wmaybe-uninitialized]

Is there somebody having an idea of why I got these warnings?

Youjun Hu
  • 991
  • 6
  • 18
  • You got those warnings because you used the "-Wall -Wextra" options. – Steve May 22 '19 at 17:22
  • @Steve Yes, I try to turn on all warnings to know whether the compiler has any concerns if they are not due to bugs of the compiler itself. For this case, if I add write(*,*) size(A) before the assignment, the warning will be gone, indicating this is subtle. Maybe gfortran-8 does not like the "(re-)allocation on assignment"? – Youjun Hu May 22 '19 at 17:28
  • Note that `write(*,*) size(a)` before the assignment makes the program invalid. – francescalus May 22 '19 at 17:32
  • @francescalus It is not reasonable to query the size of an unallocated array, but gfortran does not warn about this. – Youjun Hu May 22 '19 at 17:44
  • "may be uninitialized" means that the compiler cannot tell if the entities were assigned values. These entities are internal to the compiler. If you don't want to see the warnings then use -Wno-maybe-uninitialized. – Steve May 22 '19 at 17:46
  • @Steve I just try to figure out why gfortran sees ambiguity. My guess is that gfortran considers allocatable arrays should go through some operations (e.g., allocate()) before they can appear in an assignment, i.e., gfortran consider " "(re-)allocation on assignment" as rarely used in practice. – Youjun Hu May 22 '19 at 17:57
  • Your guess is wrong. gfortran tries to minimize re-allocation if an already allocated entity is to be assigned to. It does not do anything special with an unallocated entity. Unfortunately, the allocation code is some of the more complicated code inside gfortran, and the person(s) that shoe-horned re-allocation-on-assignment into that code may have made a mistake. The gfortran documentation tells you how to contact the gfortran developers. – Steve May 22 '19 at 18:13

1 Answers1

2

This is a well known GCC bug https://gcc.gnu.org/bugzilla/show_bug.cgi?id=77504 that has been reported in many duplicates to the bugzilla (even by myself) - see the Duplicates section for the other incarnations.

As far as I understand it the actual code generated by the compiler should work correctly and it is just an incorrect warning. As Steve pointed out in the comment, use -Wno-maybe-uninitialized to hide this warning. I have included it into my build scripts as well.

  • Thanks. I actually read these bug reports before I asked my question here. I am just wondering whether some guys here can provide some insights or alternative views about why gfortran would think that way. – Youjun Hu May 22 '19 at 19:11
  • What do you want more when the Bugzilla has the information straight from the horse's mouth? If you have read it before, you should mention that in the question. – Vladimir F Героям слава May 22 '19 at 19:21
  • I should say I skim some of the bug reports and did not get a clear idea of what's going on. I expect there should be a simple explanation of why this bug (if it is) appears or what causes gfortran raise this a little misleading warning. – Youjun Hu May 22 '19 at 19:28
  • If there were a simply explanation, then the problem would have been fixed by now. As I have stated in another comment the allocation code in gfortran is some of the more complex code in the compiler. Fixing it isn't easy. – Steve May 22 '19 at 21:01