0

I am an electrical engineer and pretty familiar with C, C++, C#, Python, and MATLAB. I have also done limited coding in the ARM assembly language. I am now trying to understand some Fortran code written over 50 years ago in order to convert it to MATLAB.

I am reading this code and I am seeing the EQUIVALENCE keyword. I do not understand its purpose. What I have read states that the keyword associates the memory location of two variables. So you could declare an int and a char and make the memory location they are stored at the same via an EQUIVALENCE statement.

What I don't understand is why you would want to do this. What is the purpose of this, why not just use the one variable the whole time rather than using two different variables representing the same thing?

How does this help you when you are writing code?

I believe the version of Fortran I am using is Fortran IV with the fixed width syntax as seen on punch cards.

      DIMENSION VAR1(6)
      EQUIVALENCE (TEMP1,VAR1(1)),(TEMP2,VAR1(2))
francescalus
  • 30,576
  • 16
  • 61
  • 96
  • 2
    Plenty of things which were a "good idea" 65 years ago haven't age well. It could well be that this is the start of a good question about retro-computing. – francescalus Nov 25 '22 at 19:53
  • You also ask a second question about _implicit typing_ (not explicitly saying what type a variable has). It's better to separate questions (you may think they are related, but implicit typing is an evil which appears quite distinctly from other Fortran evils). – francescalus Nov 25 '22 at 19:57
  • I am familiar with general idea of implicit typing, based on the layout of the code I am reading it does not appear that it is being used, however "IMPLICIT NONE" is not at the top of the code. Someone told me that this code was written before implicit typing was added to Fortran. I am unsure as to whether that is true or not. – likeapancake Nov 25 '22 at 20:04
  • 2
    Implicit typing has always been in Fortran. Indeed `implicit none` is a relative 1990 newcomer. – francescalus Nov 25 '22 at 20:04
  • In that case, when you declare a variable as a character but begin the name with a character associated with another type what happens? Ie. CHARACTER IPSYS(6) – likeapancake Nov 25 '22 at 20:10
  • `CHARACTER IPSYS(6)` declares `ipsys` to be a character (array of size 6), but that doesn't seem to be related to the other variables in the question. (Characters didn't exist until Fortran 77.) – francescalus Nov 25 '22 at 20:14
  • Okay, so even though the variable name starts with an "i" which types to integer (correct?) because it was led with CHARACTER it overwrites the implicit type of IPSYS being an integer? – likeapancake Nov 25 '22 at 20:16
  • Explicit type declarations override the otherwise implicit type, yes. I've removed that from the question here, to focus on equivalence, as you'll find many other questions about implicit typing here instead. – francescalus Nov 25 '22 at 20:18
  • Please have a read of [this other answer](https://stackoverflow.com/a/36921928/3157076) and see whether it's a good start for your understanding. – francescalus Nov 25 '22 at 20:20
  • https://stackoverflow.com/questions/252552/why-do-we-need-c-unions might be of interest, but I suspect much of it will post-date the original purpose in Fortran (my guess: to minimise memory usage) – Ian Bush Nov 25 '22 at 20:20

1 Answers1

1

After a day looking at code in whatever is the current trendy language, I feel old. Fortran is old.

Fortran is about as old as what we now call computers. What we relative youngsters call modern computers and understand as programming would be completely unrecognizable to a Fortran pioneer.

Even on this site, you can see lots of questions about EQUIVALENCE and why it was used. You see many different questions and answers because it means one thing but can be used (abused in modern terms) to do many things.

Fortran 50 years ago didn't have many tools and those tools it had did a lot of work. Heck, 50 years ago Fortran didn't even have character data types and GO TO was the go to for flow control.

Equivalence, as the question notes, is about having two things share the same part of memory: storage association.

What's the use of storage association? Several uses.

For example, know you have two variables or workspaces but aren't going to use both at the same time? Don't want to waste memory with them? Use the same memory.

Want to do tricks with data representation? Use the same lump of memory and call it two things.

Want to pass lots of model parameters to a subroutine? Use a huge argument list

function pmodel(x, y, z, p, t, dt)
pmodel = x+y+sqrt(z)*p*t/dt
end function

or pass an array and have something unreadable

function pmodel(pars)
pmodel = pars(1)+pars(2)+sqrt(pars(3))*pars(4)*pars(5)/pars(6)
end function

or use an array in common and equivalence them

function pmodel()
common pars
equivalence (pars(1), x), (pars(2), y) ... ! Like our question here
pmodel = x+y+sqrt(z)*p*t/dt
end function

We live in different, and far happier, times now. With data structures, more associations, cheap memory and a care for our fellow programmers.


So you could declare an int and a char and make the memory location they are stored at the same via an EQUIVALENCE statement.

This, however, is not true. Although a integer and a real, or a double precision and a complex, for example, can be equivalenced, characters are not allowed to be equivalenced with those non-character things. (That doesn't mean people didn't and that compilers stopped them from trying, but Fortran 77 onward strictly said you couldn't.)

francescalus
  • 30,576
  • 16
  • 61
  • 96