2

A line in my code assigns an array to an unallocated array. I thought it was a bug but to my surprise it works just fine.

program test
implicit none
    real, allocatable :: x(:,:)
    real :: y(2,2)

    y = 1.
    x = y

    print*, x
end program test

How does this work in terms of memory? The = operator here just allocates and assigns at the same time? Why is this possible and why is the compiler not complaining? I am using gfortran 5.4.0.

b-fg
  • 3,959
  • 2
  • 28
  • 44
  • It might be a question on the same matter as the one linked as duplicate, but asked in a more clear way in my opinion (also easier to find). So I will leave it for the moment even though it has been marked as duplicate. – b-fg Jun 01 '17 at 07:55
  • You certainly want to keep this question. Being a duplicate is not inherently bad. It is indeed clearer in one of the aspects covered. – francescalus Jun 01 '17 at 09:09

2 Answers2

1

Since Fortran 2003, allocatable arrays will be automatically allocated (or re-allocated if the shape has changed) at run time. See for instance the Fortran 2003 features of the NAG compiler https://www.nag.com/nagware/np/r61_doc/nag_f2003.pdf or look for "realloc" in the documentation of gfortran https://gcc.gnu.org/onlinedocs/gfortran/Error-and-Warning-Options.html#Error-and-Warning-Options

Pierre de Buyl
  • 7,074
  • 2
  • 16
  • 22
0

That is correct, according to Section 7.2.1.3, "Interpretation of intrinsic assignments" of WD 1539-1, paragraph 3.

Jack
  • 5,801
  • 1
  • 15
  • 20