Search code examples
fortran

What is the purpose of EQUIVALENCE in FORTRAN?


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))

Solution

  • 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.)