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

initialisation of components of derived types

forall
Beginner
598 Views
hi,

Do the components of derived types become SAVEd automatically if they're initialised in their declaration?

eg,
------------------
module mod
type myType
real::r1=5.0
integer::i1=3
real,pointer::p1(:)=>null()
endtype myType
contains
elemental subroutine resetMyType(vDum)
type(myType),intent(inout)::vDum
type(myType)::vDef
vDum=vDef
endsubroutine resetMyType
endmodule mod
-------------------

I use this approach to automatically 'reset' components of derived types to "default' values, including nullification of all pointer components. This is convenient because the default values are stored in the type definition and I dont have to remember to update resetMyType every time I add a component to myType. This approach compiles and (seems to) work as I intended.

However 2 things just occurred to me:

1) How does the SAVE-by-initialisation feature of Fortran interact with components of derived types, especially with pointer components? Does this mean that any variable of type(myType) automatically has the SAVE attribute on its initialised components, including 'remembering' the last pointer association? This seems undesirable due to the unnecesarily more complex code that needs to be generated by the compiler (?)

2) Doesnt this violate the requirement that a PURE or ELEMENTAL procedure should not contain SAVE or DATA statements?

I am considering modifying my code by

a) removing all initialisation from all my type definitions, including =>null() - as I dont want SAVE attributes on any of my variables unless I specifically ask for it (and I also have lots of pure procedures).

b) using parameters of type(myType), eg, adding the following immediately after the type definition:

type(myType),parameter::vPar=myType(4.0,2,null())

then I initialise to default values (including null pointers) anywhere simply using
v=vDef. Before I start on this, are there any better ways?

many thanks,
dmitri


0 Kudos
3 Replies
Steven_L_Intel1
Employee
598 Views

You've found one of the odd corners of the language. This is a case where initialization does NOT confer SAVE semantics. In this case, local variable vDef gets reinitialized on each call to the routine.

The requirements are that the local (not dummy or use/host associated) variable be non-SAVEd, not be ALLOCATABLE or POINTER, and be of derived type with component initialization. In this situation, components for which default initialization is specified become defined, other components do not.

0 Kudos
forall
Beginner
598 Views
Steve,
Thanks for the quick reply.
Your first paragraph makes sense to me (and gee I am glad the Fortran standard works that way), however I am confused by your second paragraph (perhaps there is a word or two missing from there?). If you mean that these are the requirements for PURE procedures, then surely you can have allocatable/pointer local variables and local variables of derived types with un-initialised components (I used them all the time with no errors/warnings, and never expected them) - they dont cause any side effects as long as they are not SAVEd. Please explain :-)
thanks,
dmitri

0 Kudos
Steven_L_Intel1
Employee
598 Views
I was not referring to PURE procedures at all - just saying what the rules are for how component initialization works for local variables, in any procedure. I don't think there's a problem using these with PURE procedures.
0 Kudos
Reply