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.
- If I indicate
a
, the result offunction1
, the results are not correct. The first element ofa
appears to be "clustered":a
is0
,2
,3 < / Code>.
- If I indicate the result of
b
forfunction2
, then the result is correct ib
becomes1
,2
,3
. - Strangers still point to
a
tofunction1
afterfunction2
changea
is correct for the result ofa < / 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
Comments
Post a Comment