Re: Memory leak when internal pointer passed out as parameter



Daniel Rudy wrote:
At about the time of 4/3/2007 11:52 AM, Mike stated the following:

<snip>

Rational Purify checked the code, and reported memory leak on foo1
when we allocate memory. I assum e that the compiler will allocate a
new block of memory when foo1 returns. Then the memory allocated
within foo1 will remain in the system heap forever. However I have no
way to verify it.


The allocated memory will be returned to the system heap when the
program exits, if there is a memory leak.

Nowhere does the C Standard guarantee this. Yes, modern memory
protected operating systems do reclaim a program's allocated memory
after the latter's termination, but C implementations exist on systems
that're not as sophisticated or capable. A conforming C program must
not make such assumptions.

You've also ignored the case of long-running processes like UNIX
deamons. Memory leaks in such programs can cause the system memory to
be slowly eaten up.

It's always better to explicitly free any memory when you're done with
it.

<snip>

.



Relevant Pages

  • Re: How to take in a string of any size?
    ... >the contents into the allocated memory. ... nobody said the string started at the beginning of the file. ... >number of calls to realloc() isn't going to be excessive). ...
    (comp.lang.c)
  • Re: Interprocess Communication & Performance
    ... For instance if the data items are small and ... memory the performance hit from the synchronization will overwhelm things. ... to map the allocated memory in both processes. ... memcpy's then using shared file mapping I think. ...
    (microsoft.public.win32.programmer.kernel)
  • Re: will I get Memory leak..
    ... dynamically assigned memory area will i get memory leak.. ... The problem with your code, on the other hand, is that your call to strcpywrites a null character to fp, which is one position past the end of the allocated memory. ... As a result of that error, the behavior of your entire program is undefined, which means that anything could go wrong A memory leak is very definitely a possibility from making that kind of mistake, but most of the other possible consequences of that mistake are much worse than memory leaks. ...
    (comp.lang.c)
  • Re: show disassembly
    ... C++ defines three types of storage: ... The memory they occupy ... int* pOut = NULL; ... // Free allocated memory elsewhere. ...
    (microsoft.public.vc.language)
  • Re: Memory leak when internal pointer passed out as parameter
    ... new block of memory when foo1 returns. ... within foo1 will remain in the system heap forever. ... The allocated memory will be returned to the system heap when the ... free the memory when the program exits, ...
    (comp.lang.c)