Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How `realloc` work actually in the background?

People also ask

How does realloc function work?

In the C Programming Language, the realloc function is used to resize a block of memory that was previously allocated. The realloc function allocates a block of memory (which be can make it larger or smaller in size than the original) and copies the contents of the old block to the new block of memory, if necessary.

Does realloc copy old data?

The value of realloc is the new address of the block. If the block needs to be moved, realloc copies the old contents. If you pass a null pointer for ptr , realloc behaves just like ' malloc ( newsize ) '.

Does realloc work with new?

Use of realloc() realloc deallocates the old object pointed to by ptr and returns a pointer to a new object that has the size specified by size. The contents of the new object is identical to that of the old object prior to deallocation, up to the lesser of the new and old sizes.

Does realloc free the old block?

Secondly, if realloc decided to follow the first approach (i.e. allocate a new memory block), then the old block is indeed freed by realloc . In that case trying to access the original memory location leads to undefined behavior.


realloc attempts to extend your available memory range if sufficient memory is available behind it on the heap. If not then it is equivalent to malloc a block of the new size, memcpy your contents there, free the old block. This is independent of both OS and compiler and depends on the implementation of libc that you link against.

On a similar note: mremap/MREMAP_MAYMOVE (available on modern Linux) will attempt to extend your virtual mapping by the requested size. If that is not possible then it will move your mapping to a new virtual address that has sufficient VM space behind it and then extend your mapping. This is very fast if you are frequently resizing large mappings since no physical copying is done.


An implementation of realloc() may look something like the following:

void * realloc(void *ptr, size_t size)
{
    // realloc() on a NULL pointer is the same as malloc().
    if (ptr == NULL)
        return malloc(size);

    size_t oldsize = malloc_getsize(ptr);

    // Are we shrinking an allocation? That's easy.
    if (size < oldsize) {
        malloc_setsize(ptr, size);
        return ptr;
    }

    // Can we grow this allocation in place?
    if (malloc_can_grow(ptr, size)) {
        malloc_setsize(ptr, size);
        return ptr;
    }

    // Create a new allocation, move the data there, and free the old one.
    void *newptr = malloc(size);
    if (newptr == NULL)
        return NULL;
    memcpy(newptr, ptr, oldsize);
    free(ptr);
    return newptr;
}

Note that I'm calling several functions with names starting with malloc_ here. These functions don't actually exist (to the best of my knowledge) in any implementation; they're intended as placeholders for however the allocator actually performs these tasks internally.

Since the implementation of realloc() depends on these internal tools, its implementation is OS-dependent. However, the realloc() interface is universal.


If the old pointer can't be resized on place a new one is allocated, the content is copied and the old one is freed.