UNION vs EQUIVALENCE

UNION vs EQUIVALENCE

I am looking into replacing EQUIVALENCEs with UNIONs. I wrote a little program to test this with a simple example, and it worked just fine. When I looked in the debugger, I could see the two variables of different types which shared memory locations, and when I (or my code) changed one, the other changed accordingly. However, the documentation for UNION says the following:

"With union declarations, only one map declaration within a union declaration can be associated at any point in time with the storage location that they share. Whenever a field within another map declaration in the same union declaration is referenced in your program, the fields in the prior map declaration become undefined and are succeeded by the fields in the map declaration containing the newly referenced field."

This seems to say that the behavior I saw shouldn?t happen. When one variable is changed, the other should become "undefined," presumably meaning that if I were subsequently to refer to it, it would not have a defined value.

Can you explain this for me? My code fragment is as follows:

STRUCTURE /STRUCTURE1/
UNION
MAP
CHARACTER*4 C
END MAP
MAP
INTEGER*2 I(2)
END MAP
END UNION
END STRUCTURE
RECORD /STRUCTURE1/ R
INTEGER*2 TEMP
DATA R.C /'ABCD'/
TEMP = R.I(1)
R.I(1) = R.I(2)
R.I(2) = TEMP
PRINT 20, R.C
20 FORMAT(1X,A4) ! Prints 'CDAB'

5 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

Basically in general you cannot assume the packing in a union is going to put things where you want them. If you changed your map where "c" was a character*8 and the other part was an integer*2, integer*4 and another integer*2 then tried this, you see that things no longer "lined up" even though they are supposedly the same size. For a very simple example such as yours it works fine, but with more complex unions not so unless you are very careful (but the compiler cannot enforce that).

Unions are usually used to provide a structure with flexibility rather than to take the place of an equivalence. Of course you can do so, but you have to take the necessary steps to ensure everything lines up.

James

James is mistaken in this case. UNION and EQUIVALENCE are pretty much the same, and actually the same language in the manual for UNION applies to EQUIVALENCE. "undefined" means just that - not that you can later refer to the variable again and it will be "defined" with the previous value. In other words, you have no way of knowing what an undefined value will be.

That said, the implementation is that of a normal memory overlay, just like EQUIVALENCE, so the common trick of using this to interpret a value as a different type will generally work. The standard way to do this is TRANSFER, by the way.

Steve

Steve - Intel Developer Support

Steve is certainly correct, what I was trying to say was that perhaps the language in the documentation was intended to reflect packing issues that might cause "unexpected" results to be encountered. In my experience, though, "undefined" is always easily traced back to modification of another union.

I generally use unions to provide flexibility to a structure. This might have something like a:

  
structure /struct/  
 integer type  
 union  
  map  
   character c  
  end map  
  map  
   integer i  
  end map  
 end union  
end structure  
record /struct/ r  
  
if (r.type == STRUCT_STRING) then  
  use r.c...  
elseif (r.type == STRUCT_NUMERIC) then  
  use r.i...  
endif  

If I simply wanted to overlay structures, I would use something like...

  
!DEC$ PACK:1  
record /struct1/ r1  
record /struct2/ r2  
equivalence (r1, r2)  

or whatever if it is an overlay I am trying to acheive. Just a style I suppose, in both cases you have to watch alignment once you go beyond a single variable.

One point that may or may not be of concern to you, UNIONs are not standard whereas EQUIVALENCE is, as is TRANSFER, so that may be one reason not to change from what you have now.

James

Thanks for the great replies! I think I may understand the situation now, and we clearly going to stick with the EQUIVALENCEs, of which there are tens of thousands in the legacy code that we are working with.

I was waiting to be notified of responses to my question by E-mail, and was pleasantly surprised to see the responses when I visited the forum today to see whether my query was still up. I guess the e-mail notification didn?t work.

Leave a Comment

Please sign in to add a comment. Not a member? Join today