Re: Do memory allocations need to be freeed every time?



Hi

On Thu, 19 Jun 2008 02:23:05 -0700, Ravindra.B wrote:

Is freeing the allocated memory is really required every time, for a
program like this as freeing doesn’t bring in any change w.r.t memory
space (or) will it become a memleak some where down the line if it is
not freeed?

To avoid memory leaks, for each and every time you call malloc(), you
must call free() exactly once with the same pointer that was returned.

The only exceptions to this are:

1) if the call to malloc() returned NULL
2) if you are about to immediately terminate the program anyway*

* see
http://groups.google.co.uk/group/comp.lang.c/browse_thread/thread/
be39423f62c88e04/a8e1101586394d42

HTH
viza
.



Relevant Pages

  • Re: *** glibc detected *** ./a.out: double free or corruption
    ... Sometimes i have problems when freeing the memory of certain ... NULL invokes undefined behavior. ... returned from malloc and again when it is received by removeFPage. ...
    (comp.lang.c)
  • Thank You -- Thomas J. Gritzan
    ... Thomas -- Your suggestion to malloc() out a block of memory was the ... Below are some details of my memory issues ... ... As a work around solution I guessed a ram disk would solve the ... persistence will frustrate the off topic police and give them a target ...
    (comp.lang.c)
  • Re: Simple question about headers and malloc!
    ... Therefore I am making all of its declarations ... memory (using malloc) and then exit back to main. ... allocation, I get data strored from the second allocation... ...
    (microsoft.public.vc.language)
  • Re: ten thousand small processes
    ... Stack needs to be executable for the current signal trampoline ... the use of malloc() that is causing your primary ... if there is any heap memory in use at all, no matter what you do, ... either directly, as a 4M page mapping (not used for user processes, ...
    (freebsd-performance)
  • Re: Help with Enter and Leave Instructions
    ... >>> for Memory Accesses, ... > The only standard way to do it is via malloc. ... Uh, SBRK/BRK is a standard, documented system call. ... > I really don't understand the objection to using C libraries. ...
    (alt.lang.asm)