Memory leaks in standard code

Tobias Burnus burnus@net-b.de
Fri Feb 15 13:52:00 GMT 2008


FX wrote:
>>  REAL, ALLOCATABLE :: A(:)
>>    ALLOCATE(A(50))
>>  generates a memory leak, unless ALLOCATE guards against this.
>>     
That will not leak any memory. ALLOCATE never does so. If you try to 
allocate an already allocated variable, you get en error an the 
allocation status remains the same.

Using POINTER instead of ALLOCATABLE leaks memory however.

However, I think tracking missing allocations with pointers is quite 
difficult.

real, pointer :: a, b
allocate(a)
b=>a
deallocate(b)

> Yeah, I should have said something about this... we already detect two
> cases: ALLOCATE(already_allocated_array) and
> DEALLOCATE(non_allocated_array). Are there any more?
>   
These are mandated by the standard.


Dr David Ham:
> real, pointer :: A(50)
> do
>   allocate(a(50))
>   nullify(a)
> end do
You actually loose the pointer with "nullify(A)"; I think the alias 
analysis will become difficult.

In general I find such a patch useful, though I do not fully know what 
one should implement there. I think g95 has something implemented.

Tobias



More information about the Fortran mailing list