Question

How realloc work actually in the background? If there is not enough memory available at old place does this one allocating two/many memory blocks and one pointer pointing to that and other are internally linked with each other or the old region copied into new place where enough memory is available and pointer is updating to new address and deleting the old memory?

And is that realloc is Compiler/OS dependent or independent?

Was it helpful?

Solution

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top