Thread: Realloc

  1. #1
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683

    Realloc

    How does the realloc function work... Does it allocate sequence of extra mamory bloack to the existing one or does it create a complete new block and transfer the contents from the old block.. Isnt it an expensive call... Because i saw a code wherer the array started witgh the size 1 and for every char input the array was being reallocated with an extra block.. isnt this inefficient.


    your thoughts??

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Does it allocate sequence of extra mamory bloack to the existing one
    When possible.

    >or does it create a complete new block and transfer the contents from the old block
    When necessary, but only when the size increases.

    >Isnt it an expensive call
    Dynamic allocation is expensive in general.

    >isnt this inefficient
    Yes, very. That's a naive way to implement a dynamic data structure.
    My best code is written with the delete key.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > When necessary, but only when the size increases.
    The draft C99 makes no such statement.

    [#2] The realloc function deallocates the old object pointed
    to by ptr and returns a pointer to a new object that has the
    size specified by size. The contents of the new object
    shall be the same as that of the old object prior to
    deallocation, up to the lesser of the new and old sizes.
    Any bytes in the new object beyond the size of the old
    object have indeterminate values.
    You must always assume that it does
    - allocate new
    - copy lesser of old and new sizes
    - deallocate old

    In malloc memory managers which use a pooled architecture, making the block smaller can change the pool in which it should be in, then it will also move.

    It is an implementation specific optimisation which leaves it in place.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    I was talking to one of my instructors (the one that actually knows the stuff) and one of the tips he gave me was to double the size of allocation everytime.

    So say I started off with 8 bytes and fill it up, on the realloc get 16, once that fills up get 32, etc.

    Seems like a more efficent method then just getting exactly what you need.

  5. #5
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683
    so lets say i have a pointer


    char *ptr;
    chat *ptr1;


    ptr=mallcok and stuff
    ptr1=ptr;
    ptr=realloc and stuff...

    The accordingly it is possible that ptr1 is no longer pointing to the actual location of the data.. Am i right???

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Am i right???
    Yes.
    My best code is written with the delete key.

  7. #7
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    The best policy is not to deal in piddly amounts. If you think you'll need up to 20KB of memory, allocate the lot rather than starting at 50 bytes and using 100 calls to realloc.

    And use the stack for anything less than a few kilobytes of memory.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. did i understood right this explantion of realloc..
    By transgalactic2 in forum C Programming
    Replies: 3
    Last Post: 10-24-2008, 07:26 AM
  2. writing a pack-style function, any advices?
    By isaac_s in forum C Programming
    Replies: 10
    Last Post: 07-08-2006, 08:09 PM
  3. using realloc
    By bobthebullet990 in forum C Programming
    Replies: 14
    Last Post: 12-06-2005, 05:00 PM
  4. segfault on realloc
    By ziel in forum C Programming
    Replies: 5
    Last Post: 03-16-2003, 04:40 PM
  5. Realloc inappropriate for aligned blocks - Alternatives?
    By zeckensack in forum C Programming
    Replies: 2
    Last Post: 03-20-2002, 02:10 PM