Fortran pointer functions: why does this code's behavior depend on the order of function calls? -


reference

The toy Fortran code below gives the call to two indicator function. That is, both functions return a pointer, in fact, they are both array indicators. They both try to do the same thing, which is to return an integer array pointer that refers to an integer array having three elements, 1, 2, and 3. The first function pointer assignment operator (=>) to point to the function pointer to point to an allocated array that holds the data, the second function allocates the block of dynamic memory, through the pointer, to store the data. The calling program only prints the elements of the returned array.

Here I feel weird.

  1. If I indicate a , the result of function1 , the results are not correct. The first element of a appears to be "clustered": a is 0 , 2 , 3 < / Code>.
  2. If I indicate the result of b for function2 , then the result is correct i b becomes 1 , 2 , 3 .
  3. Strangers still point to a to function1 after function2 change a is correct for the result of a < / Code> followed by 1 , 2 , 3 .

question

Why does this happen? The more precise, the pointer function that gives the pointer to the first element of the array for the collar that is an allocated array? The more precise still creates a side-effect on the second indicator ( a ), indicating an indicator ( b ), where the targets are from different functions, Which are written to not interact with each other?

Cavets

Let me run this Intel laptop with Ubuntu, using the GNU Fortran compiler v.4.3.3. Your results may vary, which can still be more interesting Finally, as always, this may be an operator error in my part, which will be least interesting to me.

code

  Any integer contained in the program main, dimension (:), indicator :: a, b integer :: iia => Function1 () b = & gt; Function 2 () Do I = 1, Print 3, A (i) End! What i = 1, 3! Print *, b (i)! End function function1 () integer, amplitude (:), allocated, target :: array integer, dimension (:), indicator :: function1 allocated (array (3)) array (1) = 1 array (2) = 2 array (3) = 3 function1 = & gt; Array end function function1 function function2 (integer), amplitude (:), indicator :: function2 allocated function2 (3)) function2 (1) = 1 function2 (2) = 2 function2 (3) = 3 end function function2 main The variable of Function1 is a local variable   

array , this address is " In plain sight of "keeping, but after leaving the function variable does not meaningfully address Once unchecked. A possible implementation is that the array of function1 will be placed on the stack, and when during the function1 returns, that area of ​​the stack will be free for other uses. But this is probably an estimate on an implementation - the important point is that you do not have permission to use the pointer value after the variable is undefined. Allocated variables are then automatically distributed when they go out of the area, unless you declare them with the "Save" feature.


Comments

Popular posts from this blog

asp.net - Javascript/DOM Why is does my form not support submit()? -

sockets - Delphi: TTcpServer, connection reset when reading -

javascript - Classic ASP "ExecuteGlobal" statement acting differently on two servers -