Thread: realloc and array notation?

  1. #1
    Unregistered
    Guest

    realloc and array notation?

    This is probably a simple question but I'd like a little clarification.
    Do these two code pieces do the same thing with the same result?
    Code:
    int array[2] = {1,2};
    int *temp;
    
    temp = realloc(array, 3 * sizeof array);
    if(temp != NULL)
        array = temp;
    array[2] = 3;
    printf("%d", array[2]);
    and
    Code:
    int *array;
    int *temp;
    
    array = malloc(2 * sizeof *array);
    if(array != NULL){
        array[0] = 1;
        array[1] = 2;
    }
    temp = realloc(array, 3 * sizeof *array);
    if(temp != NULL)
        array = temp;
    array[2] = 3;
    printf("%d", array[2]);
    Both of these pieces should change the size of the array to 3, put the integer 3 into the third element and print it without any access violation errors, right?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    > Do these two code pieces do the same thing with the same result?
    No
    The first example is illegal - you can't resize an array

    The second example does as you suggest

  3. #3
    The Artful Lurker Deckard's Avatar
    Join Date
    Jan 2002
    Posts
    633
    From the realloc() manual page:
    Unless ptr is NULL, it must have been returned by an earlier call to malloc(), calloc() or realloc().

    In your case, 'array' is what the man page is referring to as 'ptr'. Its address must have been returned by a previous call to malloc(), calloc(), or realloc().

    Also, 'array' is on the stack in the first example, and is a pointer to the heap in the second. If realloc() cannot find the contiguous memory it needs after 'array', it will allocate enough memory elsewhere, copy the contents of 'array' to the new location, and then (try to) free the memory originally used by 'array' (thus trashing the stack in the first example).

    Since a pointer (temp) is pushed onto the stack after array, we know there isn't enough room to simply expand the size of 'array', leading to the trashed stack.

    The second example should work fine, whereas the first will core dump.
    Jason Deckard

  4. #4
    Just one more wrong move. -KEN-'s Avatar
    Join Date
    Aug 2001
    Posts
    3,227
    The first is illegal, and...

    whenver you multiply by sizeof *array, you really shouldn't. you're just wasting space...
    example:
    temp = realloc(array, 3 * sizeof array);

    see, an integer is 4 bytes
    *array is an integer pointer (4 bytes)
    3 x 4 = 12 when all you wanted is 3, right?
    so that works out better as:
    temp = realloc(array, 3);

    same with anywhere else you multiply by sizeof *array.


    //edit: wow, I just posted this, and in the time it took me to write this 3 other people had replied when before there was noone! weird....

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    You got them the wrong way round Ken

    sizeof array is sizeof int*
    sizeof *array is sizeof int - array is dereferenced, and then the size is taken

    > temp = realloc(array, 3);
    and this is just 3 bytes, not 3 integers

  6. #6
    Just one more wrong move. -KEN-'s Avatar
    Join Date
    Aug 2001
    Posts
    3,227
    right...feeling stupid now...

    :smacking self: I figured out why I was thinking that, it's because the only time I've ever had to use malloc() or realloc() was with char's....I was just thinking along those lines...

    ...still feeling stupid...

  7. #7
    Unregistered
    Guest
    Wow, a lot of replies. So I think enough people have agreed that the first way is wrong and the second is right, I have to create the array with malloc to use realloc and resize it.

    After it's allocated I can use array notation to access the data, right?

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    > After it's allocated I can use array notation to access the data, right?
    yes you can

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Realloc Strings Array
    By lautarox in forum C Programming
    Replies: 41
    Last Post: 12-12-2008, 12:09 PM
  2. using realloc for a dynamically growing array
    By broli86 in forum C Programming
    Replies: 10
    Last Post: 06-27-2008, 05:37 AM
  3. How to Realloc 2-D array
    By wots_guge in forum C Programming
    Replies: 12
    Last Post: 04-14-2006, 12:56 PM
  4. Realloc problems with sturcture array inside structure
    By daveyand in forum C Programming
    Replies: 2
    Last Post: 03-29-2004, 06:48 AM
  5. Realloc inappropriate for aligned blocks - Alternatives?
    By zeckensack in forum C Programming
    Replies: 2
    Last Post: 03-20-2002, 02:10 PM