# Subroutine for allocation upon assignment

**URL:** <https://fortran-lang.discourse.group/t/subroutine-for-allocation-upon-assignment/373>\
**Category:** Uncategorized\
**Created:** [October 28, 2020, 1:32am UTC](https://fortran-lang.discourse.group/t/subroutine-for-allocation-upon-assignment/373 "2020-10-28T01:32:33Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Beliavsky](https://avatars.discourse-cdn.com/v4/letter/b/ba8739/32.png) [@Beliavsky](https://fortran-lang.discourse.group/u/Beliavsky)\
**Post date:** [October 28, 2020, 1:32am UTC](https://fortran-lang.discourse.group/t/subroutine-for-allocation-upon-assignment/373/1 "2020-10-28T01:32:33Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ivanpribec](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/ivanpribec/32/3290_2.png) [@ivanpribec](https://fortran-lang.discourse.group/u/ivanpribec)\
**Post date:** [October 28, 2020, 10:35am UTC](https://fortran-lang.discourse.group/t/subroutine-for-allocation-upon-assignment/373/3 "2020-10-28T10:35:45Z")

</div>

> [@Beliavsky](#):
>
> (2) Do you use this feature, or do you allocate arrays explicitly before setting their values? What I do is write

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.

---

<div class="post-metadata">

**Author:** ![lkedward](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/lkedward/32/72_2.png) [@lkedward](https://fortran-lang.discourse.group/u/lkedward)\
**Post date:** [October 28, 2020, 10:47am UTC](https://fortran-lang.discourse.group/t/subroutine-for-allocation-upon-assignment/373/4 "2020-10-28T10:47:37Z")

</div>

> [@ivanpribec](#):
>
> 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.

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!

---

<div class="post-metadata">

**Author:** ![billlong](https://avatars.discourse-cdn.com/v4/letter/b/71e660/32.png) [@billlong](https://fortran-lang.discourse.group/u/billlong)\
**Post date:** [October 28, 2020, 7:53pm UTC](https://fortran-lang.discourse.group/t/subroutine-for-allocation-upon-assignment/373/6 "2020-10-28T19:53:46Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![FortranFan](https://avatars.discourse-cdn.com/v4/letter/f/96bed5/32.png) [@FortranFan](https://fortran-lang.discourse.group/u/FortranFan)\
**Post date:** [October 29, 2020, 11:38am UTC](https://fortran-lang.discourse.group/t/subroutine-for-allocation-upon-assignment/373/7 "2020-10-29T11:38:29Z")

</div>

> [@Beliavsky](#):
>
> Do all currently maintained Fortran compilers implement this properly? I believe it was problematic for some time.

@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](https://www.fortranplus.co.uk/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.

---

<div class="post-metadata">

**Author:** ![shahmoradi](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/shahmoradi/32/3151_2.png) [@shahmoradi](https://fortran-lang.discourse.group/u/shahmoradi)\
**Post date:** [October 30, 2020, 4:05am UTC](https://fortran-lang.discourse.group/t/subroutine-for-allocation-upon-assignment/373/8 "2020-10-30T04:05:16Z")

</div>

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.
