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

Construction of structure with nested derived-type components

FlyingHermes
New Contributor I
1,153 Views
Hi,

I've a problem to construct a scalar structure (derived-type variable) containing some allocatable derived-type variables.
The type definition and declaration of the structure looks something like that:
[fortran]Type :: MyType
type(MySubType1) ,dimension(:) ,allocatable :: SubVar1
type(MySubType2) :: SubVar2
End Type

Type :: MySubType1
real ,dimension(:) ,allocatable :: rvar
integer ,dimension(:) ,allocatable :: ivar
End Type

Type :: MySubType2
type(MySubSubType1) ,dimension(:) ,allocatable :: SubVar1
type(MySubSubType2) ,dimension(:) ,allocatable :: SubVar2
End Type

type(MyType) :: Var
[/fortran] As you can see, in the present example the Var variable is a scalar structure containing two derived-type components, one scalar and one allocatable array called SubVar1 and SubVar2 respectively.

The problem is that I first construct the SubVar1 and SubVar2 variables and then I store them into the larger structure by simply copying the data using :
[fortran]Var%SubVar2 = SubVar2[/fortran]
However, by doing so, the components in SubVar2 are not allocated in the Var variable.
How can I transfert the allocation status into a new variable.
Thanks.



0 Kudos
3 Replies
Steven_L_Intel1
Employee
1,153 Views
Would you please show us a complete sample program that demonstrates the problem? What you have written here should work, as far as I can see.
0 Kudos
FlyingHermes
New Contributor I
1,153 Views
Please, disreagared the previous post since I have found the (stupide) mistake which was creating the problem.

The problem was simply that I used an intent(out) statment where I should have an intent(inout).
Indeed, I do not directly copying the sub-structure into the upper structure but I am using a procedure which automatically load the sub-structure into the correct structure component. The loading procedure is the following:
[fortran]Subroutine Load_SubStr ( Str, SubStr ) implicit none type(Str_Type) ,intent(inout) :: Str class(*) ,intent(in) :: SubStr select type (SubStr) type is (MyType1); Str%SubStr1 = SubStr type is (MyType2); Str%SubStr2 = SubStr end select End Subroutine[/fortran] The bug was that te Str argument had the intent(out) so that each new component I was loading into the structure was easing the old component which have previousely been loaded into the structure.



0 Kudos
Steven_L_Intel1
Employee
1,153 Views
Glad to hear it. This is an of example why posting paraphrases and snippets often hides the real problem.
0 Kudos
Reply