Thread: did i understood right this explantion of realloc..

  1. #1
    Banned
    Join Date
    Oct 2008
    Posts
    1,535

    did i understood right this explantion of realloc..

    "realloc:

    Now suppose you've allocated a certain number of bytes for an array but later find that you want to add values to it. You could copy everything into a larger array, which is inefficient, or you can allocate more bytes using realloc, without losing your data.

    realloc takes two arguments. The first is the pointer referencing the memory. The second is the total number of bytes you want to reallocate.

    Passing zero as the second argument is the equivalent of calling free.

    Once again, realloc returns a void pointer if successful, else a NULL pointer is returned. "

    i understood that realloc stitches a new sector of empty cell data to and old one.

    first we have an old array of 5 integers cells
    ptr = calloc(5, sizeof(int));

    we fill it with data and then we want to expand it with 7 new integer cells
    we take the old ptr and this command by 7 integers cells

    ptr = realloc(ptr, 7*sizeof(int));

    is it correct?
    Last edited by transgalactic2; 10-24-2008 at 07:13 AM.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, but it's not guaranteed that the new data is actually in the same piece of memory that your old data was. realloc knows the inner workings of malloc and free, so it can "look" to see if there's spare space after the current allocation, in which case it uses that memory. Otherwise it uses the method of "allocate new memory, copy and free old location" mechanism.

    Note also that calloc() zeros the content, realloc leaves it as "whatever it is" - which may be zero or any other value, depending on what has been in that memory before it was freed (or if it's a debug build, it may intentionally fill it with non-zero values to identify applications that use the content without filling it in).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Maybe.
    1. Realloc may move everything, if it is unable to find enough room at the current location (like malloc, realloc guarantees that the space provided is contiguous).
    2. Doing
      Code:
      ptr = realloc(ptr, 7*sizeof(int));
      does not add seven new cells, it adds two new cells to get a total of seven.

  4. #4
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. writing a pack-style function, any advices?
    By isaac_s in forum C Programming
    Replies: 10
    Last Post: 07-08-2006, 08:09 PM
  2. using realloc
    By bobthebullet990 in forum C Programming
    Replies: 14
    Last Post: 12-06-2005, 05:00 PM
  3. segfault on realloc
    By ziel in forum C Programming
    Replies: 5
    Last Post: 03-16-2003, 04:40 PM
  4. Realloc inappropriate for aligned blocks - Alternatives?
    By zeckensack in forum C Programming
    Replies: 2
    Last Post: 03-20-2002, 02:10 PM
  5. realloc realloc realloc
    By Linette in forum C++ Programming
    Replies: 4
    Last Post: 01-19-2002, 09:18 PM