Re: realloc() implicit free() ?



Walter Roberson wrote:
In article <118stl2rjj6c5d5@xxxxxxxxxxxxxxxxxx>,
SM Ryan  <wyrmwif@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx> wrote:

# The question is not over-specified if one is concerned about
# whether one is leaking memory. When your datasets are ~1 Gb each
# and you are handling them in a loop, you can't afford to allow memory
# to leak.

All you can do to prevent leaks is to match frees to allocs; you don't need to
know how the library is implemented to do that much. It's still leaking
you're stuck unless you can get the library source code.

That's not ALL you can do: you can also ask questions about paragraphs that one might have overlooked or which might have been clarified in addendums that one hasn't seen, which proscribe the behaviour of library routines so as to remove the ambiguity.

If no-one happens to know the answer, or if the answer is
indeterminate, then one can sometimes write one's own routine that
avoids the indeterminate behaviour. In the case of realloc(),
since it is never -required- that the address be left unchanged,
one can write a replacement for realloc() in terms of malloc(),
a memory copy, and a free(), provided that one knows the current
size of the object at hand.

For the address to not change, the new realloc'd size must fit in the same place. If there was occupied memory following your data and you wanted to increase the size of your data, the realloc will have to copy it all to a different location where it will fit. In this case, the address *will* change. .



Relevant Pages

  • Re: realloc() implicit free() ?
    ... ># whether one is leaking memory. ... of library routines so as to remove the ambiguity. ... In the case of realloc(), ...
    (comp.lang.c)
  • "Simple" animation in C# --> memory leaks???
    ... I am trying to write a "simple" animation using C#, ... things but nothing seems to work for me without leaking memory. ... "sprites" where one gets painted to the buffer each tick and the buffer ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: strange memory leak problem
    ... is leaking memory (if you're relying on CrtMemCheckPoint, ... my essay on this problem on my MVP Tips site). ...
    (microsoft.public.vc.mfc)
  • Re: what kind of command or tool is good to check memory leaking?
    ... somez72 came up with this when s/he headbutted the keyboard a moment ago in ... I made simple linux application. ... I'd like to check whether if application is leaking memory or not? ...
    (comp.os.linux.misc)
  • Re: is this a memory leak ?
    ... > We were suspecting that our driver is some how leaking memory, ... > (in our driver we use only kmalloc for memroy allocation) but what we ... > recover memory, ie at the time of the OOM killer if the free memory ... Right, if the kernel is leaking memory, the only ...
    (comp.os.linux.development.system)