# Still trying to resolve this equivalence issue

**URL:** <https://fortran-lang.discourse.group/t/still-trying-to-resolve-this-equivalence-issue/2752>\
**Category:** Help\
**Created:** [February 8, 2022, 10:24pm UTC](https://fortran-lang.discourse.group/t/still-trying-to-resolve-this-equivalence-issue/2752 "2022-02-08T22:24:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![garynewport](https://avatars.discourse-cdn.com/v4/letter/g/898d66/32.png) [@garynewport](https://fortran-lang.discourse.group/u/garynewport)\
**Post date:** [February 8, 2022, 10:24pm UTC](https://fortran-lang.discourse.group/t/still-trying-to-resolve-this-equivalence-issue/2752/1 "2022-02-08T22:24:52Z")

</div>

I have tried using pointers, etc but I cannot shake this problem.

The one issue is that I cannot see how these two variables are being equated.

So, I have two parameters defined as…

```auto
integer, parameter :: mj = 4000
integer, parameter :: me = 9

```

Later, I have two arrays defined as…

```auto
double precision :: hydrogen(mj), element(mj, me)

equivalence (element, hydrogen)

```

So, I know that `hydrogen` is now an array with 4000 indices and `element` is an array with 4000 x 9 = 36000 elements.

I understand that the `equivalence` relates `hydrogen(mj)` to `element(mj, 1)`; is that it?

When the system steps through the second dimension of `element (me > 1)`, does this mean there is no `equivalence` there?

Basically, if I was to change the contents of `element(200, 1)` then the contents of `hydrogen(200)` would also alter. Yet, if I were to alter the contents of `element(200, 2)` or `element(200,3)` or `element(200,4)` this would have no impact upon `hydrogen` at all?

Am I correct there?

---

<div class="post-metadata">

**Author:** ![garynewport](https://avatars.discourse-cdn.com/v4/letter/g/898d66/32.png) [@garynewport](https://fortran-lang.discourse.group/u/garynewport)\
**Post date:** [February 8, 2022, 11:27pm UTC](https://fortran-lang.discourse.group/t/still-trying-to-resolve-this-equivalence-issue/2752/3 "2022-02-08T23:27:32Z")

</div>

I hadn’t, actually, no.

It’s such an obvious to do, but if I might blame being focussed on too many things right now…?

Thank you. I’ll have a go once the current run of my code has finished. 🙂

---

<div class="post-metadata">

**Author:** ![mecej4](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/mecej4/32/855_2.png) [@mecej4](https://fortran-lang.discourse.group/u/mecej4)\
**Post date:** [February 9, 2022, 11:09am UTC](https://fortran-lang.discourse.group/t/still-trying-to-resolve-this-equivalence-issue/2752/4 "2022-02-09T11:09:19Z")

</div>

Using EQUIVALENCE in new code is something to avoid. Modifying old code that contains EQUIVALENCE can be troublesome. In either case, one has to read the appropriate Fortran manuals/standards to understand the rules regarding this old feature, which was quite useful in the days when many large mainframe computers had as little as 32 kWords of memory, but is best avoided these days.

Here is a modified version of your example that illustrates how, in the presence of EQUIVALENCE, an error with regard to the upper bound of one array can cause changes to the other array in ways that may be unexpected and surprising.

```auto
program OverRun
implicit none
integer, parameter :: MH = 4, ME = 3
!
integer :: hyd(MH), elems(MH,ME)
equivalence (hyd,elems) ! hyd overlays first column of elems
!
integer i,j,m,n
!
do i=1,MH
   hyd(i) = i+1000
end do
do j=2,ME
   do i=1,MH
      elems(i,j) = i*1000+j*100
   end do
end do
print '(i8,5x,3I8)',(hyd(i),elems(i,:),i=1,MH)
call update(hyd,7) ! 7 is in error; should have been just 4 or MH
print *
print '(i8,5x,3I8)',(hyd(i),elems(i,:),i=1,MH) ! Only first column of elems should have changed
end program

subroutine update(elem,n) ! triple values in elem
implicit none
integer i,n,elem(n)
elem = 3*elem
return
end subroutine

```

The program text may suggest that the call to UPDATE only changes the first column of ELEMS. However, because of passing the wrong value of n to the subroutine, the second column of ELEMS also gets changed, even though ELEMS itself was not passed to the subroutine as an argument.
