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

Is it safe to associate a global pointer to a local target variable?

S__MPay
Beginner
532 Views

If I define a local target variable inside a subroutine, then associate a global pointer to that value, is it safe to use the pointer to access that value outside of subroutine or the data can be overwritten and changed unexpectedly?

 

0 Kudos
1 Solution
Arjen_Markus
Honored Contributor I
532 Views

No, if it is a local variable, it will disappear upon return from the routine. The exception is if that variable has the SAVE attribute.

What is the reason you want to do that? There are probably better methods, depending on the purpose.

 

View solution in original post

0 Kudos
8 Replies
Arjen_Markus
Honored Contributor I
533 Views

No, if it is a local variable, it will disappear upon return from the routine. The exception is if that variable has the SAVE attribute.

What is the reason you want to do that? There are probably better methods, depending on the purpose.

 

0 Kudos
S__MPay
Beginner
532 Views

As you know type components cannot be a target, i.e. property of a class cannot be a target.

on the other hand I need properties of numerous objects to be pointing to that specific property.

My temporary solution was to make a target in a subroutine and have the pointers to associated with that value.

0 Kudos
jimdempseyatthecove
Black Belt
532 Views

>>on the other hand I need properties of numerous objects to be pointing to that specific property

Can the specific property in question .NOT. be an encapsulated property of some other object type? IOW all objects requiring said common property have pointers to the shared property.

Jim Dempsey

0 Kudos
IanH
Black Belt
532 Views

S. MPay wrote:

As you know type components cannot be a target, i.e. property of a class cannot be a target.

Component definitions cannot include TARGET, but any subobject of an object that has the target attribute is also a target.

Something associated with a pointer component is also always a target.

Perhaps you could elaborate further about the background to your question.

0 Kudos
S__MPay
Beginner
532 Views

jimdempseyatthecove wrote:

...Can the specific property in question .NOT. be an encapsulated property of some other object type?...

Dear Jim encapsulation is not a must. I am thinking to use a variable outside of target definition inside class module to workaround this. Maybe then even add a pointer type component associated with that variable inside type definition if needed.

ianh wrote:

...Perhaps you could elaborate further about the background to your question.

So my project has a Database class which keeps all the model information including an array of materials available.

We have numerous (say one million) elements but we have a few material types.

Each element has to be associated with a material.

I don't want to have one million instances of materials. So in my design each element has a material pointer which is associated with one of the materials in the Database%materialsArray.

Currently my only solution is to define materialsArray  outside of the Database type as a target array.

 

0 Kudos
IanH
Black Belt
532 Views

Just give the Database object the TARGET attribute.

TYPE :: material_type
   ...
END TYPE material_type

TYPE :: database_type
  TYPE(material_type) :: materials(nm)
  ...
END TYPE database_type

TYPE :: element_type
  TYPE(material_type), POINTER :: material
  ...
END TYPE element_type

TYPE(database_type), TARGET :: Database
TYPE(element_type) :: Elements(ne)
...
Elements(ie)%material => Database%materials(im)

 

0 Kudos
S__MPay
Beginner
532 Views

ianh wrote:

Just give the Database object the TARGET attribute.

TYPE :: material_type
   ...
END TYPE material_type

TYPE :: database_type
  TYPE(material_type) :: materials(nm)
  ...
END TYPE database_type

TYPE :: element_type
  TYPE(material_type), POINTER :: material
  ...
END TYPE element_type

TYPE(database_type), TARGET :: Database
TYPE(element_type) :: Elements(ne)
...
Elements(ie)%material => Database%materials(im)

I see, Thanks;

but the database is pretty big and contains many other types, and large arrays.

Making them all target won't affect calculation performance?

0 Kudos
IanH
Black Belt
532 Views

I don't know, as I don't know your code.  But I rather doubt it.

0 Kudos
Reply