Like Tree1Likes
  • 1 Post By grumpy

Problems understanding realloc

This is a discussion on Problems understanding realloc within the C Programming forums, part of the General Programming Boards category; Hi I have the following MWE: Code: #include <stdio.h> #include <stdlib.h> int main() { int *old_ptr = malloc(sizeof(int)); // ALLOCATE ...

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    140

    Problems understanding realloc

    Hi

    I have the following MWE:

    Code:
     
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
        int *old_ptr = malloc(sizeof(int)); // ALLOCATE ARRAY
        old_ptr[0] = 0;                     // ASSIGN A VALUE
    
        int *new_ptr = realloc(old_ptr, 2*sizeof(int)); // CREATE A NEW ARRARY new_ptr, WHICH HAS TWICE THE SIZE OF old_ptr
    
        printf("%d\n%d\n\n", old_ptr, new_ptr); // THEY HAVE THE SAME ADDRESS?!
        printf("%d", new_ptr[0]);               // THEY HAVE THE SAME ADDRESS?!
    
        return 0;
    }
    What I don't understand is that new_ptr and old_ptr have the same address in my program? That is what printf shows.. I thought new_ptr would point to a new memory block. Where am I wrong here?

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    2,671
    relloc trys to extend the location pointed to by old_ptr, if it fails it trys to allocate a new block and sets new_ptr to the new block location.
    When new_ptr equals old_ptr (and they are not NULL/0) the block was successfully extended in size.

    Tim S.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,429
    There is nothing preventing malloc() from allocating more memory than needed, so a subsequent call of realloc() doesn't necessarily need to allocate a new memory block.

    A common memory allocation strategy is to allocate in larger blocks (say, rounded up to the next power of two). However, different libraries (associated with different compilers) employ different strategies.

    Also, don't use %d to print out the value of a pointer. Convert the pointer to a void pointer, and use %p.
    Right 98% of the time, and don't care about the other 3%.

  4. #4
    Registered User
    Join Date
    Aug 2009
    Posts
    140
    Quote Originally Posted by stahta01 View Post
    relloc trys to extend the location pointed to by old_ptr, if it fails it trys to allocate a new block and sets new_ptr to the new block location.
    When new_ptr equals old_ptr (and they are not NULL/0) the block was successfully extended in size.

    Tim S.
    Thanks. So in general the behavior of realloc is

    1) The extension is small such that the old block merely increases its size, but doesn't change its location (new_ptr = old_ptr)
    2) The extension is large, such that the old block increases both its size and changes location (new_ptr != old_ptr)
    3) An error occurs (new_ptr = NULL)

    ?

    The reason why I ask is because I have seen many different explanations of what is actually going on.

  5. #5
    Stoned Witch Barney McGrew's Avatar
    Join Date
    Oct 2012
    Location
    astaylea
    Posts
    420
    Quote Originally Posted by Niels_M View Post
    Thanks. So in general the behavior of realloc is

    1) The extension is small such that the old block merely increases its size, but doesn't change its location (new_ptr = old_ptr)
    2) The extension is large, such that the old block increases both its size and changes location (new_ptr != old_ptr)
    realloc is free to move the memory around. It may do it all the time, none of the time, randomly, or when certain conditions are met. It's entirely up to the C library that implements it.

    3) An error occurs (new_ptr = NULL)
    realloc will return a null pointer on failure.

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,429
    Not in general, no. In specific implementations, maybe. There is no rule that says realloc() will only do a reallocation if it needs to.

    You are also forgetting that realloc() can be used to request reductions in the allocated size, and may interact with the operating system in different ways (eg making use of virtual memory, memory mapped files, etc).
    Right 98% of the time, and don't care about the other 3%.

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    2,671
    I am trying for a truth table here; not sure the values are all correct.

    Tim S.

    Code:
    new_ptr     old_ptr     new_size    Meaning
    NULL        NULL        non-zero    Failed to allocate space
    NON-NULL    NULL        non-zero    Allocated Space
    ???         NON-NULL    0           Freed Space
    old_ptr     NON-NULL    non-zero    Allocated Space without moving base
    new Value   NON-NULL    non-zero    Allocated Space with moving base
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  8. #8
    Registered User
    Join Date
    Aug 2009
    Posts
    140
    Quote Originally Posted by stahta01 View Post
    I am trying for a truth table here; not sure the values are all correct.

    Tim S.
    Good idea. I would say it looks like this, but I may be very wrong:

    Code:
    new_ptr     old_ptr     new_size    Meaning
    ------------------------------------------
    old_ptr     NON-NULL    non-zero    Allocated Space without moving base
    new Value   NULL        non-zero    Allocated Space with moving base
    NULL        NON-NULL    non-zero    Failed to allocate space
    NON-NULL    NON-NULL    0           Freed Space
    1) I tested out "Allocated Space without moving base" explicitly, old_ptr does not change.
    2) "Allocated Space with moving base" follows from the definition of realloc(), same with "Failed to allocate space"
    Last edited by Niels_M; 03-10-2013 at 05:29 AM.

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    6,429
    Not quite, Tim.

    1) if the new size is zero, realloc() either returns NULL or some other pointer that cannot be dereferenced without causing undefined behaviour.

    2) If the new size is equal to the old size, realloc() can return NULL (and the pointer passed is not released). [That said, I've yet to encounter any implementation that does this].

    3) If a non-NULL pointer supplied to realloc() was not returned by malloc()/calloc()/realloc(), the result is undefined behaviour. (I've lost track of the number of times I've seen folks trying to use realloc() to resize a static or auto array).

    4) There is nothing preventing realloc() from allocating new memory and moving data even if it doesn't actually need to.
    stahta01 likes this.
    Right 98% of the time, and don't care about the other 3%.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble understanding Malloc, Calloc, and Realloc
    By matthayzon89 in forum C Programming
    Replies: 18
    Last Post: 10-19-2010, 12:20 AM
  2. Unknown Seg Fault and Malloc/Realloc Problems
    By DonFord81 in forum C Programming
    Replies: 6
    Last Post: 12-01-2008, 10:49 PM
  3. Understanding malloc and realloc functions
    By mariano_donati in forum C Programming
    Replies: 16
    Last Post: 02-18-2008, 10:36 PM
  4. realloc() problems
    By DMH in forum C Programming
    Replies: 19
    Last Post: 10-27-2005, 02:01 PM
  5. realloc problems
    By Benzakhar in forum C++ Programming
    Replies: 2
    Last Post: 12-31-2003, 11:31 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21