# Declare automatic arrays using shape of argument, without using size intrinsic

**URL:** <https://fortran-lang.discourse.group/t/declare-automatic-arrays-using-shape-of-argument-without-using-size-intrinsic/6993>\
**Category:** Language enhancement\
**Created:** [December 14, 2023, 3:28pm UTC](https://fortran-lang.discourse.group/t/declare-automatic-arrays-using-shape-of-argument-without-using-size-intrinsic/6993 "2023-12-14T15:28:45Z")\
**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:** [December 14, 2023, 3:28pm UTC](https://fortran-lang.discourse.group/t/declare-automatic-arrays-using-shape-of-argument-without-using-size-intrinsic/6993/1 "2023-12-14T15:28:45Z")

</div>

I wish automatic arrays whose shapes are the same as that of a procedure argument could be declared more concisely. If `x` is a 3-D procedure argument you can write

`real :: y(size(x,1), size(x, 2), size(x, 3))`

but it is verbose, and it’s possible to slip and unintentionally write

`real :: y(size(x,1), size(x, 2), size(x, 2))`

More concise and less error-prone would be

`real :: y(shape(x))`

This would be allowed if the rank of x is known at compile time, as it usually is. Another possible syntax is

`real, mold(x) :: y`

I know you can make `y` a local allocatable array and write

`allocate (y, mold=x)`

A similar thread was [How to concisely declare a function result as a multi-dimensional automatic array?](https://fortran-lang.discourse.group/t/how-to-concisely-declare-a-function-result-as-a-multi-dimensional-automatic-array/4476)

---

<div class="post-metadata">

**Author:** ![everythingfunctional](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/everythingfunctional/32/176_2.png) [@everythingfunctional](https://fortran-lang.discourse.group/u/everythingfunctional)\
**Post date:** [December 14, 2023, 9:15pm UTC](https://fortran-lang.discourse.group/t/declare-automatic-arrays-using-shape-of-argument-without-using-size-intrinsic/6993/2 "2023-12-14T21:15:26Z")

</div>

> [@Beliavsky](#):
>
> real :: y(shape(x))

This is in fact valid now (I believe).

> 8.5.8.2 Explicit-shape array  
> …  
> C833 An explicit-bounds-expr shall be a restricted expression that is a rank one integer array with constant size.

It comes in with handful of the “rank-agnostic” syntax in F2023

---

<div class="post-metadata">

**Author:** ![gnikit](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/gnikit/32/1213_2.png) [@gnikit](https://fortran-lang.discourse.group/u/gnikit)\
**Post date:** [December 14, 2023, 11:44pm UTC](https://fortran-lang.discourse.group/t/declare-automatic-arrays-using-shape-of-argument-without-using-size-intrinsic/6993/3 "2023-12-14T23:44:45Z")

</div>

I should **really** read the F2023 draft when I get the chance. This is the second time in a week I am caught off guard about a new feature of Fortran, the first one was `split`.

---

<div class="post-metadata">

**Author:** ![everythingfunctional](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/everythingfunctional/32/176_2.png) [@everythingfunctional](https://fortran-lang.discourse.group/u/everythingfunctional)\
**Post date:** [December 15, 2023, 2:25am UTC](https://fortran-lang.discourse.group/t/declare-automatic-arrays-using-shape-of-argument-without-using-size-intrinsic/6993/4 "2023-12-15T02:25:17Z")

</div>

I gave a brief overview recently.

[![](https://global.discourse-cdn.com/free1/uploads/fortran_lang/original/2X/f/fb4397ed40e3b41aacf48c4a16caf224ceea3789.jpeg "FUN Office Hours November 7th, 2023") ](https://www.youtube.com/watch?v=EbJunVcl83s)

> [@Beliavsky](#):
>
> real :: y(shape(x))

While this is now valid, I’m still hesitant to recommend its use in declaring dummy arguments. Nothing verifies that the actual argument is the same shape. It can lead to weird bugs for multi-dimensional arrays, or flat out memory corruption in any case.

---

<div class="post-metadata">

**Author:** ![PierU](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/pieru/32/1848_2.png) [@PierU](https://fortran-lang.discourse.group/u/PierU)\
**Post date:** [December 15, 2023, 2:19pm UTC](https://fortran-lang.discourse.group/t/declare-automatic-arrays-using-shape-of-argument-without-using-size-intrinsic/6993/5 "2023-12-15T14:19:18Z")

</div>

> [@Beliavsky](#):
>
> real :: y(shape(x))

And what if one wants to specify lower bounds ? is `real :: y(lbound(x):shape(x))` possible ?

---

<div class="post-metadata">

**Author:** ![everythingfunctional](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/everythingfunctional/32/176_2.png) [@everythingfunctional](https://fortran-lang.discourse.group/u/everythingfunctional)\
**Post date:** [December 15, 2023, 3:03pm UTC](https://fortran-lang.discourse.group/t/declare-automatic-arrays-using-shape-of-argument-without-using-size-intrinsic/6993/6 "2023-12-15T15:03:25Z")

</div>

> [@PierU](#):
>
> And what if one wants to specify lower bounds ? is `real :: y(lbound(x):shape(x))` possible ?

```auto
real :: y(lbound(x):)

```

is valid for assumed shape, and if you do still want to go the explicit shape route, one would do

```auto
real :: y(lbound(x):ubound(x))

```
