Array size after free
I created a distributable array. I am highlighting the elements and then printing the size of the array. It seems strange to me that the size remains the same after freeing.
Integer, Allocatable :: fred(:)
Allocate (fred(3))
Write (*,*) "fred: ", Size (fred)
Deallocate (fred)
Write (*,*) "fred: ", Size (fred)
source to share
This is a question that appeals to the canon, indeed. In the interest of answering your specific question in the absence (as far as I can tell, but I can write it in the end), I will answer.
The argument size
must not be an unallocated allocable variable.
For your code, fred
is the variable to be allocated. If this block of code is executed, then the final line size
contains an argument that is an unallocated delimiter variable. In this case, if it is part of a program (program unit), then this program (program unit) is not a standard compatible program (program unit).
This mismatch is not a mismatch that a Fortran processor needs to find a compatible Fortran processor.
Yes, it would be nice if the processor detects this, and many will if you choose the appropriate options at compile time. The standard response processor will be allowed in the popular language for the outbreak of World War II in response to this code. It can also print 3
. It depends entirely on the whim of the compiler vendor.
Comment hint, additional information.
It is tempting, perhaps, to expect the size of the freed array to be zero. However, a freed array and an array with zero elements are very different things, since a zero-length character is not the same as an unallocated allocated character.
Specifically, we have idioms like
fred = [fred, append]
which are invalid when fred
not allocated, but when allocated but has a size of zero; and in this latter case, it does not need special treatment.
I agree with High Performance Mark's comment that if the compiler is going to return any value, it 0
is a bad choice. Since the size is undefined, any attempt to access fred(3)
, say, based on the size returned is also a bad idea. Again, the compiler can give any specific meaning for this reference.
Finally, if you want to check if an array has been allocated, you should use the inline allocated
rather than relying on the size
returning one 0
. Of course, in this case it is not necessary, as you can be absolutely sure that after the operator deallocate
fred
is not really assigned.
In Fortran 90, it was possible that the distribution status would be undefined and then allocated
not even allowed.
source to share