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

Threaded View

Previous Post Previous Post   Next Post Next Post
  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.

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