Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Beginner
2 Views

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

Jump to solution

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

Accepted Solutions
Highlighted
Valued Contributor II
2 Views

No, if it is a local variable

Jump to solution

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
Highlighted
Valued Contributor II
3 Views

No, if it is a local variable

Jump to solution

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
Highlighted
Beginner
2 Views

As you know type components

Jump to solution

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
Highlighted
2 Views

>>on the other hand I need

Jump to solution

>>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
Highlighted
Black Belt
2 Views

Quote:S. MPay wrote:

Jump to solution

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
Highlighted
Beginner
2 Views

Quote:jimdempseyatthecove

Jump to solution

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
Highlighted
Black Belt
2 Views

Just give the Database object

Jump to solution

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
Highlighted
Beginner
2 Views

Quote:ianh wrote:

Jump to solution

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
Highlighted
Black Belt
2 Views

I don't know, as I don't know

Jump to solution

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

0 Kudos