# Unexpected allocatable behaviour

**URL:** <https://fortran-lang.discourse.group/t/unexpected-allocatable-behaviour/5510>\
**Category:** Help\
**Created:** [April 5, 2023, 7:50pm UTC](https://fortran-lang.discourse.group/t/unexpected-allocatable-behaviour/5510 "2023-04-05T19:50:01Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![fedebenelli](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/fedebenelli/32/1823_2.png) [@fedebenelli](https://fortran-lang.discourse.group/u/fedebenelli)\
**Post date:** [April 5, 2023, 7:50pm UTC](https://fortran-lang.discourse.group/t/unexpected-allocatable-behaviour/5510/1 "2023-04-05T19:50:01Z")

</div>

I’m trying to better understand allocatable, something that I don’t fully understand and feel like could introduce a ton of hard-to-find errors is that I can access any index even if I haven’t allocated the array up to that dimension. A simple example would be this:

```fortran
program main
    implicit none
    real, allocatable :: x(:, :)
    integer :: i

    allocate(x(2, 2))

    x(1, :) = [1, 2]
    x(2, :) = [3, 5]

    do i=1, 10
        print *, x(i, :)
    end do

end program main

```

which outputs:

```auto
  1.00000000 2.00000000
   3.00000000 5.00000000
   0.00000000 0.00000000
   2.00000000 1.68919523E-40
   5.00000000 0.00000000
   0.00000000 0.00000000
   1.68919523E-40 0.00000000
   0.00000000 0.00000000
   0.00000000 0.00000000
   0.00000000 0.00000000

```

I would expect an error after `i=3` but that’s not the case. Is there any way to avoid this behavior?

---

<div class="post-metadata">

**Author:** ![hkvzjal](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/hkvzjal/32/3055_2.png) [@hkvzjal](https://fortran-lang.discourse.group/u/hkvzjal)\
**Post date:** [April 5, 2023, 7:58pm UTC](https://fortran-lang.discourse.group/t/unexpected-allocatable-behaviour/5510/2 "2023-04-05T19:58:34Z")

</div>

Hi! yes, there is the “-check bounds” (ifort) flag that can help you out! this might take a bit of performance out of your execution but the code will indeed be much more reliable

 ![image](https://global.discourse-cdn.com/free1/uploads/fortran_lang/original/2X/5/5e873e145a76e28cd2889fd34ce3122d5ad8cb32.png)

Without the flag:  
 ![image](https://global.discourse-cdn.com/free1/uploads/fortran_lang/original/2X/2/2a2c654852cf50313516bea5ae64db572f02869f.png)

For gfortran check this thread: [fortran - gfortran -fbounds-check doesn't always work? - Stack Overflow](https://stackoverflow.com/questions/37557026/gfortran-fbounds-check-doesnt-always-work)

---

<div class="post-metadata">

**Author:** ![msz59](https://avatars.discourse-cdn.com/v4/letter/m/3d9bf3/32.png) [@msz59](https://fortran-lang.discourse.group/u/msz59)\
**Post date:** [April 5, 2023, 8:17pm UTC](https://fortran-lang.discourse.group/t/unexpected-allocatable-behaviour/5510/3 "2023-04-05T20:17:44Z")

</div>

Fortran does not have any automatic bounds check. And it has nothing to do with allocatables. You will get similar results with a “static” array.

---

<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:** [April 5, 2023, 8:24pm UTC](https://fortran-lang.discourse.group/t/unexpected-allocatable-behaviour/5510/4 "2023-04-05T20:24:20Z")

</div>

> [@fedebenelli](#):
>
> I’m trying to better understand allocatable, something that I don’t fully understand and feel like could introduce a ton of hard-to-find errors is that I can access any index even if I haven’t allocated the array up to that dimension. A simple example would be this: …

@fedebenelli ,

Your concerns are legit, 'most everyone can empathize with you and relate to your needs.

Under the circumstances, you may want to consider the following:

1. see if it is viable for you to use more than Fortran compiler to test your code and luckily in this respect, you have the advantage of tapping into gfortran, Intel Fortran, LFortran, etc.
2. view the compiler as two different ones, a debugging compiler and an optimizing one. Use the former during your development and the latter to achieve peak performance. Then with the debugging compiler (e.g., with `-g` compiler option), look into the available options such as with Intel Fortran’s _-check:bounds_, _-check:pointer_, etc. to test drive your code. Other compilers may offer similar or other options, check their documentation.
3. Also, see if **[defensive programming](https://en.wikipedia.org/wiki/Defensive_programming)** is possible for you. Fortran language offers some “tools” for the same. For example, with the code in the original post, you can do

```fortran
   real, allocatable :: x(:, :)
   integer :: i

   allocate(x(2, 2))

   x(1, :) = [1, 2]
   x(2, :) = [3, 5]

   if ( allocated(x) ) then 
      do i = lbound(x,dim=1), ubound(x,dim=1)
        print *, x(i, :)
      end do
   end if

end

```

which will output:

```auto
C:\temp>gfortran -ffree-form p.f -o p.exe

C:\temp>p.exe
   1.00000000 2.00000000
   3.00000000 5.00000000

```

Best,

---

<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:** [April 6, 2023, 6:59am UTC](https://fortran-lang.discourse.group/t/unexpected-allocatable-behaviour/5510/5 "2023-04-06T06:59:11Z")

</div>

Runtime checks like bound-checking can be computationaly expensive, and Fortran is traditionnally targeting high performance computing. That’s why the standard doesn’t require the compilers to perform runtime checks, and that’s why the compilers do not apply them by default.

---

<div class="post-metadata">

**Author:** ![vmagnin](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/vmagnin/32/28_2.png) [@vmagnin](https://fortran-lang.discourse.group/u/vmagnin)\
**Post date:** [April 6, 2023, 7:26am UTC](https://fortran-lang.discourse.group/t/unexpected-allocatable-behaviour/5510/6 "2023-04-06T07:26:14Z")

</div>

A good point is that Fortran arrays begin at 1 by default, so bound errors are less frequent than in C… 😀

---

<div class="post-metadata">

**Author:** ![JohnCampbell](https://avatars.discourse-cdn.com/v4/letter/j/5daacb/32.png) [@JohnCampbell](https://fortran-lang.discourse.group/u/JohnCampbell)\
**Post date:** [April 6, 2023, 8:26am UTC](https://fortran-lang.discourse.group/t/unexpected-allocatable-behaviour/5510/7 "2023-04-06T08:26:08Z")

</div>

Hi @fedebenelli,

Your “which outputs:” shows output that is different from @hkvzjal’s expected output, without any “check bounds” , as for i=3 you are reporting 0 0. This is surprising.  
Can you report the compiler, OS and compile options you used ?

I am not sure if @FortranFan 's approach helps, as to me, the allocate(x(2,2)) is fairly clear. I can’t see any justification for suggesting the x(2,2) allocated status has changed.

As for using i \> 2, although it is out of bounds, the memory referencing should not produce the unusual results you are reporting.  
I would expect x(3,1) would be the equivalent to array address x(1,2), as based on your code example, there is no reason to suggest the array is not using contiguous memory.
