Subroutine for allocation upon assignment

In Fortran 2003 onward,

integer, allocatable :: ivec(:)
ivec = [10,20]

allocates ivec to a 2-element array and sets its value. No ALLOCATE statement is needed. I have some questions and comments about this.

(1) Do all currently maintained Fortran compilers implement this properly? I believe it was problematic for some time.

(2) Do you use this feature, or do you allocate arrays explicitly before setting their values? What I do is write

call set_alloc([10,20],ivec)

where set_alloc is a subroutine I have created. I do this to avoid possible compiler bugs and because I would like array allocations to stand out.

(3) It’s easy to write a set_alloc subroutine and overload it for various data types and ranks, but should the language itself have an intrinsic subroutine that does this for all cases? Admittedly it would be redundant because of the existing allocation upon assignment syntax.

For arrays populated with literals, I sometime use the automatic allocation. More often I just use the allocate explicitly. A custom subroutine for allocation can be useful if you want to handle the stat and errmsg in some way.

For creating a copy of an array variable, I also like the allocate(a,source=b) form, which should be the same as a = b if I am not mistaken. I recall that with older versions of gfortran you can get some spurious warning messages.

1 Like

Yes I also use this syntax (allocate(a,source=b)) sometimes to avoid spurious gfortran warnings or side-step odd bugs in older versions. There is one difference to be aware of: allocation on assignment will reallocate silently which can catch you out!

Some compilers used to have a non-default option to “turn on” the auto-allocate feature; it was non-default because otherwise there were performance complaints. More recent releases (especially since claims of F2008 conformance) have made the auto-allocate mode the default and still have a non-default option to turn it off.

2 Likes

@Beliavsky,

Welcome to this forum.

You can best determine the answer to your first question both by actually trying out and testing the compilers of interest to you and by consulting information online such as Fortranplus | Fortran information - see “Fortran Resources” pdf file there.

You may note it’s been over 15 years since Fortran 2003 was published and thankfully, there are enough processors that support most (or all) of this standard revision.

As to “allocation upon assignment”, you will find gfortran and Intel Fortran has been providing support for this for quite some time in their releases.

Considering the current language standard (Fortran 2018) and the state of compilers, you should not have to worry about your questions 2 and 3.

To my knowledge, this was the default behavior in the Intel compiler up to 2017. Thereafter, the default has been Fortran 2008 (if not 2018). In my opinion, one should always use the latest features. If the features are not used by the developers, the vendors won’t feel any pressure to implement them.