Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29261 Discussions

Structure constructor with UNION ... END UNION

kramer__walter
Beginner
937 Views
Hello,

I get the following error message for a structure constructor of a derived type:

error #8212: Omitted field is not initialized. Field initialization missing: [RR143]

However, RR143 is not an additional field, but amapping. Am I missing something or is this a compiler problem?
Before Iused equivalence to mapan instance of the derived type and the array representationin the same memory. Since this presented serious problems (which are actually resolved in 11.1.051) I tried the UNION..END UNION, which obviously resulted in a different kind of problem. What would be the preferred method anyway, equivalencing or using UNION .. ENDUNION (provided the issue with the structure constructor is resolved)?

The constructor looks like this:

RC143=ST143(' ',(/' ',' ',' '/),0.,0.,0.,0.,0., &
0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0)

and the derived type declaration looks like this:

TYPE ST143
SEQUENCE
UNION
MAP
CHARACTER(4) IDF
CHARACTER(4) NAME(3)
REAL(4) TFL1
REAL(4) TFLX
REAL(4) DTMX
REAL(4) FFL
REAL(4) CPFL
REAL(4) UA
REAL(4) WFH
REAL(4) XT
REAL(4) DUA
REAL(4) TFL2
REAL(4) WCFL
REAL(4) WCS
REAL(4) CPS
REAL(4) XNTU
REAL(4) CAR
REAL(4) EFF
REAL(4) TS2
REAL(4) XLMTD
INTEGER(4) IP2
END MAP
MAP
REAL(4) RR143(23)
END MAP
END UNION
END TYPE
TYPE(ST143) RC143



Walter Kramer

0 Kudos
5 Replies
jimdempseyatthecove
Honored Contributor III
937 Views

TYPE ST143_FIELDS
SEQUENCE
CHARACTER(4) IDF
CHARACTER(4) NAME(3)
REAL(4) TFL1
REAL(4) TFLX
REAL(4) DTMX
REAL(4) FFL
REAL(4) CPFL
REAL(4) UA
REAL(4) WFH
REAL(4) XT
REAL(4) DUA
REAL(4) TFL2
REAL(4) WCFL
REAL(4) WCS
REAL(4) CPS
REAL(4) XNTU
REAL(4) CAR
REAL(4) EFF
REAL(4) TS2
REAL(4) XLMTD
INTEGER(4) IP2
END TYPE ST143_FIELDS

TYPE ST143
UNION
MAP
TYPE(ST143_FIELDS) ::F
END MAP
MAP
REAL(4) RR143(23)
END MAP
END UNION
END TYPE ST143

TYPE(ST143) RC143


RC143%F=ST143_FIELDS(' ',(/' ',' ',' '/),0.,0.,0.,0.,0., &
0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0)

Jim Demspey

0 Kudos
kramer__walter
Beginner
937 Views

Hello Jim,

Thanks for the answer. Is this the way the developers have actually intended it or is it aworkaround?
It would mean that I now have to refer to a member of the structure as: RC143%F%FIELDNAME.
In that case I prefer the methodwith EQUIVALENCE.
I have also looked at the possibility to use TRANSFER (which may be feasible under some circumstances), however this results in more memory use and in my opinion to unnecessary copying of data. Moreover, the mold for copying data from the structure to a one dimensional array is fairly straightforward (RR143=TRANSFER(RC143, (/23*0.0/)), however if I want to copy the data back I need the mold based on structure ST143_FIELDS (and I have many of these structures all with different composition), or can I also use the type name "ST143_FIELDS" to represent the mold?

Walter Kramer

0 Kudos
jimdempseyatthecove
Honored Contributor III
937 Views

Try a pointer

[cpp]TYPE ST143_FIELDS
  SEQUENCE
    CHARACTER(4) IDF    
    CHARACTER(4) NAME(3)
    REAL(4) TFL1
    REAL(4) TFLX
    REAL(4) DTMX
    REAL(4) FFL
    REAL(4) CPFL
    REAL(4) UA
    REAL(4) WFH
    REAL(4) XT
    REAL(4) DUA
    REAL(4) TFL2
    REAL(4) WCFL
    REAL(4) WCS
    REAL(4) CPS
    REAL(4) XNTU
    REAL(4) CAR
    REAL(4) EFF
    REAL(4) TS2
    REAL(4) XLMTD
    INTEGER(4) IP2
END TYPE ST143_FIELDS

TYPE ST143_UNION
  UNION
    MAP
      TYPE(ST143_FIELDS) :: F
    END MAP
    MAP
      REAL(4) RR143(23)
    END MAP
  END UNION
END TYPE ST143_UNION

TYPE(ST143_UNION)    RC143_UNION
TYPE(ST143_FIELDS), POINTER :: RC143
...
RC143 => RC143_UNION ! done once
... RC143=ST143_FIELDS(' ',(/' ',' ',' '/),0.,0.,0.,0.,0., & 0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0) ...
RC143%CPS = 12.34


Jim Dempsey



[/cpp]
0 Kudos
Steven_L_Intel1
Employee
937 Views
This is the intended behavior. UNION, an extension, doesn't play well with certain standard language features, notably structure constructotrs. We spent a lot of time thinking about this exact issue and decided that the current behavior was the "least offensive", as there are some big problems that can occur if the behavior was otherwise.
0 Kudos
kramer__walter
Beginner
937 Views

Thanks Jim, Steve

The issue is clear to me now,

Walter Kramer

0 Kudos
Reply