REALLOC AND FREE

Discussion in 'C Programming' started by vivek, Mar 14, 2008.

  1. vivek

    vivek Guest

    Hi,

    If i allocate some memory using malloc(bigger size memory) and then
    reallocate the same pointer returned by malloc(for a smaller size
    memory), what happens to the remainng memory??? When is it
    available???

    Is the user free to do pointer manipulation to access the remaining
    memory???

    Then after i use realloc, when i free the pointer, how does it
    react???

    Does it release the bigger size memory???

    If no, what happens to the remaining memory???
    vivek, Mar 14, 2008
    #1
    1. Advertising

  2. vivek said:

    > Hi,
    >
    > If i allocate some memory using malloc(bigger size memory) and then
    > reallocate the same pointer returned by malloc(for a smaller size
    > memory), what happens to the remainng memory??? When is it
    > available???


    Given a pointer to the first byte in a block of memory successfully
    allocated by malloc, you can pass that pointer to realloc, which may be
    able to change the size of the allocated block. If realloc returns a null
    pointer, the change failed. Otherwise, the pointer value it returns should
    be used *instead of* your original pointer, which is no longer valid. If
    the block shrank, as in your question, the amount by which it shrank no
    longer belongs to your program and must not be used.

    It may be that the pointer returned by realloc happens to be the same as
    the original pointer (that is, realloc was able to do the resizing "in
    place"), or it may be that it isn't (because realloc chose to reposition
    the block, in which case it will copy the contents of the old block to the
    new block, up to the size of the smaller of the two).

    Either way, you mustn't use the old pointer and you mustn't use the memory
    you no longer own.

    So here's how we normally do it, for some object type T, such that 'new'
    and 'old' are both of type T *, and 'old' points to the first byte in a
    block successfully allocated with malloc. n is the new number of objects
    we wish to allocate:

    T *new = realloc(old, n * sizeof *old);
    if(new != NULL)
    {
    old = new;
    new = NULL;
    }
    else
    {
    handle the failure in some appropriate way
    }

    > Is the user free to do pointer manipulation to access the remaining
    > memory???


    If you mean the memory that the program used to own and no longer owns, the
    answer is no - the behaviour is undefined if you do that.

    > Then after i use realloc, when i free the pointer, how does it
    > react???


    As you would expect. It marks the allocated block as being available for
    further allocation, which means that it's no longer available to you
    (except by calling malloc again, and you might get that block back or you
    might not, and it might have the same data in it as when you released it
    or it might not). In general, if you still need the memory, don't free it.
    If you free it, don't expect to see it again.

    > Does it release the bigger size memory???


    No, because that was already handled during the realloc call.

    > If no, what happens to the remaining memory???


    As far as your program is concerned, it vanishes. If you want the same
    block back, don't free it in the first place. (But it isn't a memory
    "leak" - the block becomes available for further allocation.)

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
    Richard Heathfield, Mar 14, 2008
    #2
    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. Jun Woong

    Re: Override malloc,calloc,realloc and free?

    Jun Woong, Jun 26, 2003, in forum: C Programming
    Replies:
    0
    Views:
    1,079
    Jun Woong
    Jun 26, 2003
  2. Dan Pop
    Replies:
    0
    Views:
    899
    Dan Pop
    Jun 26, 2003
  3. Douglas A. Gwyn

    Re: Override malloc,calloc,realloc and free?

    Douglas A. Gwyn, Jun 26, 2003, in forum: C Programming
    Replies:
    0
    Views:
    747
    Douglas A. Gwyn
    Jun 26, 2003
  4. Dan Pop
    Replies:
    2
    Views:
    2,052
    Jun Woong
    Jun 27, 2003
  5. Jun Woong

    Re: Override malloc,calloc,realloc and free?

    Jun Woong, Jun 27, 2003, in forum: C Programming
    Replies:
    0
    Views:
    842
    Jun Woong
    Jun 27, 2003
Loading...

Share This Page