Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

locally initialized pointers?

Status
Not open for further replies.

ya0037

Programmer
Oct 7, 2010
30
0
0
DE
Link
"The section (5.2.3) of the F2008 standard referring to the implicit SAVE attribute does not distinguish between the cases of POINTER or not-POINTER."
If in my program (subroutines or functions) I have local pointer variables like
Code:
 real function f(v)   
 real, dimension(:), intent(in) :: v   
 !local variables
 real, dimension(:), pointer :: ke => null()
 integer :: n
 !
 n=size(v,1)   
 allocate(ke(n))   
 !make a copy   
 ke=v   
 !do some computation ...   
 end function f

which would be allocated at the initial part of the routine or
pointed to other pointers or
targets or
module procedures
I should not initialize the local pointer variable with => null()?
Or it is different for module procedures or etc.

 
In that case the SAVE attribute is strictly equivalent to the declaration with ke => null() but both seem useless because you immediately modify ke via the ALLOCATE statement. What is the interest to save something which will be never used ?

In addition, be careful : I don't see the corresponding DEALLOCATE statement here (potential memory leak).

I always prefer not to allocate variables declared pointer (I only allocate variables declared ALLOCATABLE). I just use pointer variable to point to (more exactly to alias) a memory zone already allocated (either statically or dynamically).

François Jacq
 
Thanks a lot @François Jacq for your useful comments.
I have seen that different compilers have different behavior in this.
For example, it seems that intel compiler has the save attribute per default while Gnu compiler does not.
To be completely on the safe side as I asked before:

If a pointer variable points to a memory zone (which is already allocated or will be allocated either statistically or dynamically by calling the procedure) I do not need to initialize the pointer!

 
I have seen that different compilers have different behavior in this.
For example, it seems that intel compiler has the save attribute per default while Gnu compiler does not.

??? I don't understand. The standard warrants that a local variable of a subroutine or function (for a module this is another story),pointer or not , keeps its value from a previous call if it has the SAVE attribute or if it is declared with an initialization expression (which implies the SAVE attribute). That's all and all compilers apply that rule !

Now, if a non saved variable keeps its value from a previous call, then this is just a question of chance and your program must not rely on that. If your program tries to use that value, then it is not standard conforming : it will possibly work using a particular compiler but not with another one : this is not the fault of the second compiler but just YOUR fault because you have not programmed correctly !

François Jacq
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top