How does realloc know how much to copy?
Asked Answered
A

5

13

how does realloc know the size of original data?

 void *realloc(void *ptr, size_t size);

So, if the implementation is like this:

 temp = malloc(size);
 memcpy(.. // How much to copy?
 free(ptr);
 return temp;

I realize this is not the original implementation, and realloc doesn't always do free, but when it does, how much does it copy?

Edit: Thanks for the answers. But how can I then implement realloc in my code with malloc/free/..?

Acaulescent answered 13/8, 2010 at 11:43 Comment(1)
Your implementation of realloc will have to follow the implementation of malloc and free. Implementing it without the special knowledge that malloc and free have is not really doable. If you did get it to work with one malloc implementation it probably wouldn't work with others.Zygospore
B
24

It knows because malloc recorded that information when you called it. After all, the system has to keep track of the sizes of allocated blocks anyway so that it doesn't allocate a particular region of memory twice.

If you mean, "how does it know how much of the array I've written in so far", it doesn't need to. It can just copy any uninitialised garbage as well.

Bevan answered 13/8, 2010 at 11:45 Comment(1)
Depends on the implementation. You can safely assume it's not accessible to you in any portable way.Flypaper
T
3

But how can I then implement realloc in my code with malloc/free/..?

If you're already using malloc & free, why not just use realloc? else you can just have a look at the CRT source that ships with MSVC/gcc etc. (or just download it, in the case of GCC), and see how they implement it. If your running a custom allocator, then its a little more situational, eg: I use a binary bin with a slab type system, in which case realloc is simple:

void* Reallocate(Manager* pManager, void* pBlock, size_t nSize, const char* szFile, const DWORD dwLine)
{
    #if ( MMANAGER_NULL_TO_DEFAULT )
        if(pManager == NULL)
            pManager = MMANAGER_DEFUALT_MANAGER;
    #endif

    if(pBlock == NULL)
        return Allocate(pManager,nSize,szFile,dwLine);
    else if(nSize == 0)
    {
        Free(pManager,pBlock,szFile,dwLine);
        return NULL;
    }

    BlockHeader* pHeader = GetHeader(pBlock);
    size_t nPrevSize = pHeader->pPoolBlock->nSize;
    if(nPrevSize < nSize)
    {
        void* pNewBlock = Allocate(pManager,nSize,szFile,dwLine);
        memcpy(pNewBlock,pBlock,nPrevSize);
        PoolBlock* pPoolBlock = pHeader->pPoolBlock;
        if(pPoolBlock == NULL)
            free(pHeader);
        else
            FreeBlock(pPoolBlock,pHeader);

        return pNewBlock;
    }

    return pBlock;
}
Thordia answered 13/8, 2010 at 12:36 Comment(0)
B
1

realloc (and malloc and free) have full access to the entire datastructure that makes up the heap. In that datastructure is information about the sizes of blocks, which realloc needs to know, and so does free.

Bethelbethena answered 13/8, 2010 at 11:46 Comment(0)
V
1

When you malloc some memory, the block you get is usually a fixed offset into a larger data structure that also holds extra information, notably the size of the block. You can verify that this is true on some systems by just noting that every address returned by malloc ends in 8 when printed in hex (e.g., with the %p substitution to printf). Of course, realloc can reverse this offset and get back to the memory management structure, and so get the size; from there, being able to know how much to copy (when necessary) is trivial…

Vaisya answered 13/8, 2010 at 11:58 Comment(0)
T
1

Why don't you just look up how malloc/calloc/realloc/free is implemented in the C standard library you're using?

Or, if you don't have access to the source code, look at how it's implemented in one of the open-source C standard libraries.

Tildatilde answered 13/8, 2010 at 12:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.