Ron Shepard
2020-03-06 16:00:08 UTC
I have a question about the usage of the MOVE_ALLOC() intrinsic. Suppose
I have a stack implemented as
type stack_type
type(stack_type), allocatable :: previous
...other data
end type stack_type
type(stack_type), allocatable :: stack, temp
Suppose that I have some entries that have been pushed onto the stack,
and I want to pop the last entry from the stack. This might be done as
call MOVE_ALLOC( from=stack%previous, to=temp )
call MOVE_ALLOC( from=temp, to=stack )
or as
call MOVE_ALLOC( from=stack, to=temp )
call MOVE_ALLOC( from=temp%previous, to=stack )
or as
call MOVE_ALLOC( from=stack%previous, to=stack )
The first option leaves temp unallocated, while it remains allocated in
the second case (with the data associated with the last stack entry). Of
course, temp is unchanged in the last case.
Are all of those equivalent as far as popping the stack? Particularly
that last option is not clear. I can see that it *could* be implemented
so that it works correctly, but I can also see how if the deallocations
are done in the wrong order, it could fail, and I can't really tell from
reading the standard whether or not that is required to work.
If the from= and to= are the same variable, then the standard requires
that variable to be unallocated after the call, which indirectly implies
something about the order of the deallocation in that case. But I don't
know if stack%previous and stack are the same variable in standardspeak,
so I don't know if that applies.
$.02 -Ron Shepard
I have a stack implemented as
type stack_type
type(stack_type), allocatable :: previous
...other data
end type stack_type
type(stack_type), allocatable :: stack, temp
Suppose that I have some entries that have been pushed onto the stack,
and I want to pop the last entry from the stack. This might be done as
call MOVE_ALLOC( from=stack%previous, to=temp )
call MOVE_ALLOC( from=temp, to=stack )
or as
call MOVE_ALLOC( from=stack, to=temp )
call MOVE_ALLOC( from=temp%previous, to=stack )
or as
call MOVE_ALLOC( from=stack%previous, to=stack )
The first option leaves temp unallocated, while it remains allocated in
the second case (with the data associated with the last stack entry). Of
course, temp is unchanged in the last case.
Are all of those equivalent as far as popping the stack? Particularly
that last option is not clear. I can see that it *could* be implemented
so that it works correctly, but I can also see how if the deallocations
are done in the wrong order, it could fail, and I can't really tell from
reading the standard whether or not that is required to work.
If the from= and to= are the same variable, then the standard requires
that variable to be unallocated after the call, which indirectly implies
something about the order of the deallocation in that case. But I don't
know if stack%previous and stack are the same variable in standardspeak,
so I don't know if that applies.
$.02 -Ron Shepard