Reputation: 11
In Fortran coding language, what is Deallocate(x) is used for? x variable is an array. I heard it deletes all the elements in it. Is it true? One of its example:
Toplam = 0.0
DO K = 1, N
Toplam = (Ort - X(K))**2
END DO ! bellek bloğu boşaltılıyor
DEALLOCATE(X)
Std_Sap = SQRT(Toplam/(N-1))
PRINT *,"Ortalama : ",Ort
PRINT *,"Standart sapma: ",Std_Sap END PROGRAM Ortalama
Upvotes: 1
Views: 183
Reputation: 60008
The linked question When is array deallocating necessary? is very closely related and I will try to avoid repeating points raised there and strictly just answer the main point "What does deallocate()
actually do"?
Allocatable variables (scalars and arrays) are just handles or references. They do not actually refer to any part of memory untill they are allocated using allocate()
. This allocation gives them some part of memory that they occupy. At that moment the status changes from "not allocated" to "allocated". (You can query the status using the allocated()
function.)
If you have an allocated allocatable (e.g. allocatable array), you can deallocate it using deallocate()
. This deallocation takes the memory back from the variable/array. The variable is now "not allocated" and contains no data elements. It does not have any array size, you cannot read it, write to it, you cannot even print the size()
or shape()
, it is just "not allocated" - that's all. It does not any longer refer to any particular memory.
You use the word "deleted" in your question. The memory might not be overwritten and the values that had been stored there could remain there. But they no longer belong to that array and the memory may be given to some other allocatable or pointer variable.
As the linked answers show, in some situations the deallocation is performed automatially.
Upvotes: 2