Re: Allocatable Components of Derived Type



Richard Maine wrote:
Oskar Enoksson <nobody@xxxxxxxxxxx> wrote:

Richard Maine wrote:

2. You can generalize that to all uses of allocatable. Allocatables
don't cause memory leaks. Unless, of course, there is a compiler bug.
One exception I guess is an allocatable array declared in a subroutine
with the "SAVE" attribute(?)

No. There are *NO* exceptions.

Such a case will not cause a memory leak, as normally defined. Perhaps
you are talking about the fact that such an array remains allocated
after the return. That's the only thing I can even imagine that you
might be aluding to. That is true, but that does not meet the usual
definition of a memory leak. The memory in question is not "lost". If
you reenter the subroutine, it will still be accessible, that being the
whole point of SAVE. If you try to allocate the array again without
deallocating it first, you won't leak the memory of the first
allocation; instead, you will get an error.

Absent compiler bugs, allocatables do not leak memory. There are no
exceptions. This is by deliberate design. If you actually know of any
exceptions, I'd suggest turning in a defect report to the standard
committee because that would be a defect. It did take a long time to
plug all the holes. I suppose it is vaguely possible that one is left
somewhere, though I don't think so. Certainly it would be nothing as
simple and obvious as a local allocatable variable with the SAVE
attribute. That *IS* a simple case.

Yes. And no, I don't know of any unplugged hole. But do you recommend to generally trust the compiler by never deallocating an allocatable variable explicitly?

/Oskar
.



Relevant Pages

  • Re: Allocatable Components of Derived Type
    ... One exception I guess is an allocatable array declared in a subroutine ... There are *NO* exceptions. ... Such a case will not cause a memory leak, ... If you try to allocate the array again without ...
    (comp.lang.fortran)
  • Re: iso_varying_string in f2003?
    ... > possible to allocate something and then not deallocate it... ... That's not a memory leak. ... > to the array on return to the main program? ...
    (comp.lang.fortran)
  • Re: memory leak occured while running mtest from c-client
    ... It's complaining about getpwnam, which is known to allocate a buffer that it uses in subsequent calls. ... Application termination is the ultimate memory leak clean-up; at least on modern operating systems. ...
    (comp.mail.imap)
  • How to allocate a SID so that it can be freed by FreeSid()?
    ... As far as I can tell, FreeSid() should only be called on SIDs ... I want to allocate my own SID. ... memory leak and a memory overrun. ...
    (microsoft.public.platformsdk.security)
  • Re: Cons cell archaic!?
    ... from s-expression or XML or other syntax you keep the bloated array ... For using vectors to emulate lists that ... Allocate 2, move 1 element: ... What do you think of that algorithm? ...
    (comp.lang.lisp)