0

I read a Fortran code :

  program t
    real :: x(5)  =[1.,-2.,3.,-4.,5.]
    real,allocatable :: y(:)
    y = x
    ...

For me the normal way is allocate(y,source=x)

I'm surprised that it works, both compile with ifort or gfortran

Is there something new in the fortran standards that I'm not aware of?

4
  • 2
    It's a little surprising you would call allocate(y,source=x) the normal way, as y=x has worked this way for as long as sourced allocation has existed in Fortran. One notable difference between the two ways is that y must not be allocated for the allocate statement, but can for the assignment (not initialization). Commented Oct 31, 2023 at 11:36
  • Another difference being around assignment involving polymorphic variables (not the case in this example), but could be a good alternative question. Commented Oct 31, 2023 at 11:38
  • Another difference you can check for error with allocate. What good it will do you, damn lazy allocation ... Commented Oct 31, 2023 at 11:59
  • 2
    And since Fortran 2008: allocate(a,b,c,source=x) instead of a=x; b=x; c=x. Commented Oct 31, 2023 at 12:04

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.