0

I currently write a small Fortran code (90/03). I have a serious doubt with a simple syntax:

INTEGER :: BB
DOUBLE PRECISION,DIMENSION(10) :: CC
INTEGER,DIMENSION(:),ALLOCATABLE :: AA
BB=10
... (many things to declare double precision values in CC)
AA=pack([( i,i=1,BB )],mask=CC.GT.0.0) ! size(CC,1)=BB

Could I use this syntax? Do I need to allocate AA before call pack? Is there any risk of using this syntax?

0

1 Answer 1

1

The syntax is correct (except for the part that is left out where we cannot anything about it).

Could I use this syntax?

Yes.

Do I need to allocate AA before call pack?

No. Allocatable arrays will be automatically allocated and deallocated when out-of-scope. Regardless of whether the object on the right-hand is the result of any function or something else, e.g. an array of literals.

Is there any risk of using this syntax?

I cannot think of anything risky here.


I guess you are confused about this line

AA=pack([( i,i=1,BB )],mask=CC.GT.0.0)

I will try to explain it:

  1. [( i,i=1,BB )] Builds an array of integers from 1 through 10 by an implied-do loop. Lets call this array B=B(i)
  2. mask=CC.GT.0.0 Only the elements B(i) of the previous array are choosen where CC(i)>0 holds.
  3. pack(...) returns a 1-dimensional array of the previous result.
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. The pack line was clear for me but I had a serious doubt concering the auto-allocation.
@guuk old compilers may need some extra options (like -frealloc-lhs for gfortran (< 4.8?) or -assume realloc_lhs for ifort <= 16(?)). Also, so-called "polymorphic assignment" may have issues for old gfortran. So to make it work even for old compilers, it may be safer to use allocate() explicitly; otherwise, I think auto-allocation works well for newer compilers by default (like gcc >= 8 or 9, though I have no experience for polymorphic assignment yet...)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.