- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
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
Link Copied
3 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.

Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page