https://gcc.gnu.org/bugzilla/show_bug.cgi?id=90890

--- Comment #5 from Steve Kargl <sgk at troutmask dot apl.washington.edu> ---
On Sun, Jun 16, 2019 at 04:03:50PM +0000, bharat.mahajan at hotmail dot com
wrote:
> 
> Ok I agree that 'a' is undefined in this case. But that was not my concern in
> the original submission. To again clarify what my concern is, 
> 
> If the following program fails with segfault because 'a' is undefined:
> 
> program test
>      implicit  none
>      real, dimension(:), allocatable :: a    
>      integer :: b
>      a = [a, 2.0]
>      b = 100
>      print *, a
> end program test
> 
> then why the following program works and prints "2.0" on screen??

You got lucky.  You gave the compiler invalid code.  It can do
anything with the code (including giving you a result that you
may expect).

> program test
>      implicit  none
>      real, dimension(:), allocatable :: a    
>      a = [a, 2.0]
>      print *, a
> end program test
> 
> This is just very unusual to me because 'b' is unrelated to 'a' and 'a' is
> undefined in both programs. We should see segfault in both cases to be
> consistent but I am seeing that the 2nd programs works and prints "2.0" on
> windows machine with MinGW-w64's gfortran-8. I checked it on gfortran-8 on
> Ubuntu and it segfaults in both cases, so this behavior is may be specific to
> MingW-w64 project only.
> 
> If you are saying standard does not say any thing in this regard and if you 
> use
> 'a' in this manner, the behavior is undefined. So this behavior of sometimes 
> it
> works and sometimes not is ok, I hear you.

The Fortran standard says plenty.  To paraphrase, a program(mer) shall not
reference an undefined variable.  There are very few exceptions (like 2).
The only place an unallocate allocatable entity can be referenced is as
the argument to the ALLOCATED() intrinsic proceduce.  The other exception
is that an unassociated POINTER can be referenced in the ASSOCIATED() 
intrinsic procedure.

If the Fortran standard had a requirement that a compiler must detect and
report the above invalid program, then the Fortran standard would contain
a numbered constraint.  There isn't a number constraint.  F2018 contains

  5.4.10 Allocatable variables

  The allocation status of an allocatable variable is either allocated
  or unallocated. An allocatable variable becomes allocated as described
  in 9.7.1.3. It becomes unallocated as described in 9.7.3.2.

  An unallocated allocatable variable shall not be referenced or defined.


> To be honest, computer science is not my area of expertise and I have been
> doing Fortran programming part-time since February 2019, so see for yourself 
> if
> I am making any sense here!!

I'm a physicist with an area of expertise in is underwater acoustic.
In fact, I think that you'll find that none of the usual contributors
to gfortran are computer scientist.

Reply via email to