Can realloc potentially cause a memory leak?

Discussion in 'C Programming' started by Jeff Rodriguez, Nov 19, 2003.

  1. If you realloc, and the new memory has a different address, what happens at the
    old address? Is that memory basically free()'d, or do you need to keep track of
    address changes and manually free() ?

    Jeff
     
    Jeff Rodriguez, Nov 19, 2003
    #1
    1. Advertising

  2. Jeff Rodriguez

    Artie Gold Guest

    Jeff Rodriguez wrote:
    > If you realloc, and the new memory has a different address, what happens
    > at the old address? Is that memory basically free()'d, or do you need to
    > keep track of address changes and manually free() ?
    >

    It is free()-ed.

    HTH,
    --ag
    --
    Artie Gold -- Austin, Texas
    Oh, for the good old days of regular old SPAM.
     
    Artie Gold, Nov 19, 2003
    #2
    1. Advertising

  3. Jeff Rodriguez

    Morris Dovey Guest

    Jeff Rodriguez wrote:

    > If you realloc, and the new memory has a different address, what happens
    > at the old address? Is that memory basically free()'d, or do you need to
    > keep track of address changes and manually free() ?


    Jeff...

    It doesn't cause a memory leak. It works to consider that memory
    freed for re-use; but the actual (under the covers) disposition
    is up to the implementation.

    --
    Morris Dovey
    West Des Moines, Iowa USA
    C links at http://www.iedu.com/c
    Read my lips: The apple doesn't fall far from the tree.
     
    Morris Dovey, Nov 19, 2003
    #3
  4. Jeff Rodriguez

    Mac Guest

    On Tue, 18 Nov 2003 19:02:07 +0000, Jeff Rodriguez wrote:

    > If you realloc, and the new memory has a different address, what happens at the
    > old address? Is that memory basically free()'d, or do you need to keep track of
    > address changes and manually free() ?
    >
    > Jeff


    In the case you seem to be discussing, the memory is effectively freed,
    and you MUST not free it.

    Misuse of realloc CAN cause a memory leak, but only when allocation fails.

    Here's an example:


    #include <stdlib.h>
    ....
    char *foo;
    int buf_size = 1;
    ....

    foo = malloc(buf_size);
    ....

    foo = realloc(foo, buf_size *= 2);

    Here, if realloc fails, the memory previously pointed to by foo will still
    be claimed, but you will have lost your only pointer to it, because
    realloc returns NULL on failure. This is a memory leak.

    HTH

    Mac
    --
     
    Mac, Nov 19, 2003
    #4
  5. Jeff Rodriguez

    Derk Gwen Guest

    Jeff Rodriguez <> wrote:
    # If you realloc, and the new memory has a different address, what happens at the
    # old address? Is that memory basically free()'d, or do you need to keep track of
    # address changes and manually free() ?

    If the returned address is different from the input address, the input
    address is no longer valid, as if it were passed to free; it can no longer
    be safely freed, reallocked, or used to access memory. What actually happens
    is implementation dependent.

    --
    Derk Gwen http://derkgwen.250free.com/html/index.html
    This is one wacky game show.
     
    Derk Gwen, Nov 19, 2003
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Antonio
    Replies:
    1
    Views:
    1,095
    Alvin Bruney [MVP - ASP.NET]
    Jun 29, 2005
  2. NewToCPP
    Replies:
    4
    Views:
    386
    Axter
    Dec 9, 2005
  3. Yi
    Replies:
    6
    Views:
    307
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=
    Apr 18, 2007
  4. Would this cause a memory leak?

    , Aug 1, 2007, in forum: C Programming
    Replies:
    11
    Views:
    624
  5. Obnoxious User

    Will this cause a memory leak?

    Obnoxious User, Apr 2, 2008, in forum: C++
    Replies:
    4
    Views:
    254
    Sharad
    Apr 3, 2008
Loading...

Share This Page