Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does realloc() reallocate the memory?

How does realloc() reallocate the memory which was first allocated by malloc()?

I know that you need to use malloc() before you´re able to reallocate the memory, but I don´t understand how that really should work. What if a dynamic-memory object gets decreased in size by realloc()? Is this respective piece of the object just erased after the call to realloc()?


My Question is:

  1. How does the realloc() function reallocate a dynamic-memory object created by malloc()?

Note: I did this Q&A because many beginners seem to be still confused about the issue of reallocating memory using realloc() despite already existing questions here on SO for that topic. They seem to be a little confusing for anyone who is new to the topic and still do not represent the whole behavior of realloc(). Therefore, and because the questions, IMHO, still do not quite fit the answer I´d want to give, I made my own Q&A.

like image 581
RobertS supports Monica Cellio Avatar asked Jan 25 '23 10:01

RobertS supports Monica Cellio


1 Answers

Note: All citations in the following answer are quoted from the actual C standard, ISO/IEC 9899:2018 (C18), section 7.22.3.4.


First, the synopsis for the realloc() function from ISO/IEC 9899:2018, Section 7.22.3:

#include <stdlib.h> 
void *realloc(void *ptr, size_t size);

Despite its name, the realloc() function does not "reallocate" anything. realloc() is not modifying an extant object in memory. Instead, it does some sort of "create (new object) & copy the data" routine.


If size is not 0 and ptr either points to an object that was allocated by one of the memory management functions (not just malloc() only) or points to NULL, then realloc() usually creates a new object and copies the data from the old object into the new object.

*I do say usually because you can´t assume that a new object in memory is really allocated. You must always check whether or not it was allocated by checking whether the returned pointer points to NULL.


If the size of the new object is larger than the old object, the bytes of the new object that are beyond the size of the old object have indeterminate values. If the new object is shorter than the old object, the values inside the difference between are thrown away. Every other value remains in the new object as it was in the old one.

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.


After that, if:

  • ptr is not a pointer to NULL and is a pointer earlier returned by a memory management function, and the object this pointer is pointing to has not been deallocated before the call to realloc(),

    If ptr is a null pointer, the realloc function behaves like the malloc function for the specified size. Otherwise, if ptr does not match a pointer earlier returned by a memory management function, or if the space has been deallocated by a call to the free or realloc function, the behavior is undefined.

  • size is not 0,

    If size is zero and memory for the new object is not allocated, it is implementation-defined whether the old object is deallocated. If the old object is not deallocated, its value shall be unchanged.

  • and a new object could really be allocated if realloc() did not return a pointer to NULL,

    If size is nonzero and memory for the new object is not allocated, the old object is not deallocated

and really only if all of these premises are fulfilled, realloc() deallocates the memory of the old object and returns a pointer with the address of the new object in memory.

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.

If realloc() returns a pointer to NULL, no new object is created and the old object remains unchanged at its address in memory.


Optionally, to make the "pseudo-reallocating" behavior almost perfect, it is possible that the new object, after the deallocation of the old object is done (if it happens), is allocated back at the same address in memory where the old object was stored.

The realloc function returns a pointer to the new object (which may have the same value as a pointer to the old object), or a null pointer if the new object has not been allocated.

In this case, there are logically two data copying processes in realloc(), one time into a buffer object and later back to the place of where the original old object was stored. The buffer object is deallocated after the execution of realloc()is completed.


The pointer of ptr which first is used for pointing to the old object should not be used for the returned pointer. If the call statement to realloc() looks like this:

ptr = realloc(ptr,size);

then you usually have a memory leak if the reallocation fails because you just overwrote the pointer to the old memory with a null pointer. If you don't have another pointer that points to it, you've leaked the memory.

Therefore, it is usually better to use a variant on:

void *new_space = realloc(ptr, new_size);
if (new_space == NULL)
{
     /* …handle out of memory condition… */
     /* ptr is still valid and points to the previously allocated data */
     return; /* Or otherwise do not continue to the following code */
}
ptr = new_space;
size = new_size;

Note that according to what I´ve said above, the address may be the same as before the call to realloc().


To make sure that memory management is really happening that way, we can try this experiment:

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    size_t length1 = 4;
    size_t length2 = 2;

    int *ptr1 = malloc(sizeof(*ptr1) * length1);
    if(ptr1 == NULL)
    {
         printf("The object could not be allocated!\n");
         return 1;
    }  

    printf("value (not address) of ptr1 before realloc(): %p\n", (void *)ptr1);

    ptr1 = realloc(ptr1,length2);

    if(ptr1 == NULL)
    {
         printf("No new object allocated. Old object remains!\n");
         return 1;
    }

    printf("value (not address) of ptr1 after realloc(): %p\n", (void *)ptr1);

    free(ptr1);

    return 0;
}

At my try it gave the output of:

value (not address) of ptr1 before realloc(): 0x1db4010
value (not address) of ptr1 after realloc(): 0x1db4010

So, the address stored in ptr1 after the use of realloc() is equivalent to before the call of it.

Additional Notes:

  • realloc() acts as malloc() when ptr is a NULL pointer:
int *ptr = NULL;
size_t length = 4;
ptr = realloc(ptr,sizeof(*ptr) * length);

shall have the same effect as,

int *ptr;
size_t length = 4;
ptr = malloc(sizeof(*ptr) * length);

If ptr is a null pointer, the realloc function behaves like the malloc function for the specified size.

But, in my personal opinion, you should not first allocate dynamic storage by the use of realloc(). I recommend that you always use malloc() or another allocating memory management function instead. It may cause some difficulties to future readers.


  • You should not use realloc(ptr,0) as substitution for free(ptr) to deallocate the dynamic memory because it is implementation-defined whether the old object is really deallocated or not.

If size is zero and memory for the new object is not allocated, it is implementation-defined whether the old object is deallocated. If the old object is not deallocated, its value shall be unchanged.

Always use free() to deallocate a dynamically allocated object.

like image 64
RobertS supports Monica Cellio Avatar answered Jan 29 '23 06:01

RobertS supports Monica Cellio