Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can someone explain how EQUIVALENCE(A,B) works?

Status
Not open for further replies.

tralalaLinda

Programmer
Apr 29, 2013
2
SE
Hi,

I'm all new with Fortran and has been given a code to run. I'm now trying to understand all the code, but I have some problems with the EQUIVALENCE statement...

What I have understood so far is that the variables share a common memory space. But will they have the same value?

I have the code

COMPLEX*16 AA,AB
DIMENSION BA(2),BB(2)
EQUIVALENCE (AA,BA(1)),(AB,BB(1))

The variables AA and AB are then given some values, but the variables BA and BB are never given any values. Will BA and BB get the same value as AA and AB?
In the code are the variables BA(1), BA(2), BB(1) and BB(2) used, but I can't see that they are given any values.
I'm confused and I can't understand it...
 
Equivalence is a statement from ancient times back and should not be used in modern coding.
The meaning is that any entity specified in a pair of brackets refers to the same location in memory. In your code AA and BA(1) share the same memory.

Code:
EQUIVALENCE (AA, BA(1))

AA = 1.0
BA(1) = 2.0
write (*,*) AA

this would print out 2.00000 as result.

To use this statement is considered poor programming today for it makes debugging difficult. And, in fact, I do not see and never did any good reason to use this statement.

Norbert

The optimist believes we live in the best of all possible worlds - the pessimist fears this might be true.
 
Thank you for making it clear for me! It's an old code I've been given...

But in your example, will the BA(2) have any value?
 
No, BA(2) is not affected. BA(2) and all the other elements in this array have their own locations in memory and are not subject to the equivalence. But if you would want it to be then you would code (but this is extremely bad coding, and added just for clarity):

Code:
EQUIVALENCE (AA, BA(1), BA(2))

BA(1) = 1.0
AA = 2.0
write (*,*) BA(1), BA(2)

would give

2.0000 2.0000

as result.

Norbert


The optimist believes we live in the best of all possible worlds - the pessimist fears this might be true.
 
I really can't see how that can possibly work. You can equivalence two items from different arrays but not from the same array.
 
???
This is what I found on the docs, I do not use the equivalence statement, never did, for it does not make any sense to me.

My docs say:
Code:
EQUIVALENCE
Statement: Specifies that a storage area is shared by two or more objects in a program unit. This causes total or partial storage association of the objects that share the storage area. 

Syntax 

EQUIVALENCE (equiv-list) [, (equiv-list)] ... 


equiv-list 
Is a list of two or more variables, array elements, or substrings, separated by commas (also called an equivalence set). If an object of derived type is specified, it must be a sequence type. Objects cannot have the TARGET attribute. 
Each expression in a subscript or a substring reference must be an integer initialization expression. A substring must not have a length of zero.

Rules and Behavior 

The following objects cannot be specified in EQUIVALENCE statements:

A dummy argument 
An allocatable array 
A pointer 
An object of nonsequence derived type 
An object of sequence derived type containing a pointer in the structure 
A function, entry, or result name 
A named constant 
A structure component 
A subobject of any of the above objects 
The EQUIVALENCE statement causes all of the entities in one parenthesized list to be allocated storage beginning at the same storage location.

... but I must confess that my compiler issues an errormessage:
The equivalences are causing alignment problems. [BA]



But I can live with my ignorance, for as I said, I consider using equivalence () a bad programming style.
And I am sorry, if this caused a problem though...

Norbert

The optimist believes we live in the best of all possible worlds - the pessimist fears this might be true.
 
I'm not very familiar with usage of equivalence statement in Forran, but I could imagine, that it could be useful for redefinition of data structures like we very often do in COBOL.
For example when we want to fill ISO date in format 'YYYY-MM-DD' in one step into redefined data structure {YEAR, Separator, MONTH, Separator, DAY} we can do that with help of equivalence statement:
Code:
[COLOR=#a020f0]program[/color] equivalence_example
  [COLOR=#2e8b57][b]implicit[/b][/color] [COLOR=#2e8b57][b]none[/b][/color]

  [COLOR=#2e8b57][b]type[/b][/color] date_iso
     [COLOR=#2e8b57][b]sequence[/b][/color]
     [COLOR=#2e8b57][b]character[/b][/color]([COLOR=#2e8b57][b]len[/b][/color][COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]4[/color]) :: year     
     [COLOR=#2e8b57][b]character[/b][/color]        :: s1 [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]'-'[/color]    
     [COLOR=#2e8b57][b]character[/b][/color]([COLOR=#2e8b57][b]len[/b][/color][COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]2[/color]) :: month
     [COLOR=#2e8b57][b]character[/b][/color]        :: s2 [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]'-'[/color]
     [COLOR=#2e8b57][b]character[/b][/color]([COLOR=#2e8b57][b]len[/b][/color][COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]2[/color]) :: day
  [COLOR=#2e8b57][b]end type[/b][/color] date_iso

  [COLOR=#2e8b57][b]character[/b][/color]([COLOR=#2e8b57][b]len[/b][/color][COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]10[/color]) :: my_date 
  [COLOR=#2e8b57][b]type[/b][/color](date_iso) :: my_date_redefined

  [COLOR=#2e8b57][b]equivalence[/b][/color] (my_date, my_date_redefined)

  [COLOR=#0000ff]! This doesn't work:[/color]
  [COLOR=#0000ff]!my_date_redefined = '2013-05-01'[/color]
  [COLOR=#0000ff]! but this works:[/color]
  my_date [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]'2013-05-01'[/color]

  [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]'date  = '[/color], my_date
  [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]'redef = '[/color], my_date_redefined

  [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]'day   = '[/color], my_date_redefined%day
  [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]'month = '[/color], my_date_redefined%month
  [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]'year  = '[/color], my_date_redefined%year    
[COLOR=#a020f0]end program[/color] equivalence_example
Output:
Code:
$ gfortran equivalence_example.f95 -o equivalence_example

$ equivalence_example
 date  = 2013-05-01
 redef = 2013-05-01
 day   = 01
 month = 05
 year  = 2013
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top