Replacing equivalence statements

I have a few cases where I have a declaration, such as…

equivalence (element,hydrogen) , (velement,vhydrogen)

Is this not simply a replacement for using the following in the code?

element = 2
hydrogen = element

If so, is there any benefit of using equivalence (or it’s f90 version) over this simple assignment?

I’m also struggling to find a reason for bonding two variables together such that they mimic each other; am I misunderstanding equivalence?

EQUIVALENCE is a way of declaring that two (or more) variables share the same storage. Sometimes this was done to save memory, other times it was done to do “type casting” where you reinterpret the bits of a variable of one type (for example, real) as another type. It is not an assignment.

Without seeing more of the program, I can’t comment on what was intended, but EQUIVALENCE is deemed “obsolescent” and is considered bad practice nowadays.

So, my understanding was correct (rather than my notation)?

That when declaring equivalence, the two variable names are pointing to the same memory location? As one changes, the other changes as well?

Is that correct?

If so, then I need to simply explore where each is used, identify the data types (which all look as if they are double precision or real) and see if I might drop the one variable name entirely.

Yes, with a caveat that equivalencing character and non-character types has undefined behavior.

Well, in the case of element and velement; they are both classified as real(8), whilst hydrogen and vhydrogen are, by implication, both double precision.

I have now listed all the variables used within the routine, recording what it is, it’s data type, what it is used to represent (if I have found out) and where it is used. I can see that vhydrogen isn’t used at all anywhere, velement is used 3 times, hydrogen is used 12 times and element is used 7 times.

I’ll now try and see where, how and why. :slight_smile: