Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does realloc overwrite old contents?

Tags:

c

realloc

People also ask

Does realloc delete old data?

For a realloc that reduces the size of the block, the old data will be truncated. Note that your call to realloc will mean you lose your data if, for some reason the realloc fails.

What happens to old memory after realloc?

If realloc succeeds, it will take ownership of the incoming memory (either manipulating it or free ing it) and return a pointer that can be used ("owned") by the calling function. If realloc fails (returns NULL ), your function retains ownership of the original memory and should free it when it's done with it.

Do you need to free memory after realloc?

In short, no. Once you call realloc() , you do not have to free() the memory addressed by pointer passed to realloc() - you have to free() the memory addressed by the pointer realloc() returns. (Unless realloc() returns NULL , in which case the original block of memory - passed to realloc() - has to be free() 'd.)

What is the purpose of realloc ()?

The function realloc is used to resize the memory block which is allocated by malloc or calloc before. Here, pointer − The pointer which is pointing the previously allocated memory block by malloc or calloc. size − The new size of memory block.


Don't worry about the old contents.

The correct way to use realloc is to use a specific pointer for the reallocation, test that pointer and, if everything worked out ok, change the old pointer

int *oldpointer = malloc(100);

/* ... */

int *newpointer = realloc(oldpointer, 1000);
if (newpointer == NULL) {
    /* problems!!!!                                 */
    /* tell the user to stop playing DOOM and retry */
    /* or free(oldpointer) and abort, or whatever   */
} else {
    /* everything ok                                                                 */
    /* `newpointer` now points to a new memory block with the contents of oldpointer */
    /* `oldpointer` points to an invalid address                                     */
    oldpointer = newpointer;
    /* oldpointer points to the correct address                                */
    /* the contents at oldpointer have been copied while realloc did its thing */
    /* if the new size is smaller than the old size, some data was lost        */
}

/* ... */

/* don't forget to `free(oldpointer);` at some time */

It grows already-allocated memory without overwriting existing content, or (if it's unable to grow) it allocates new larger memory at a different location and copies existing contents from previous memory into new memory.


You should program as if the old pointer is overwritten, yes. The old memory is no longer allocated so can be re-allocated by another part of your program (or a system thread for example) and written over at any time after you call realloc.

The new memory will always contain the same data that was present in the old memory though (it is copied for you if necessary), but only up to the size of the old block, any extra space allocated at the end will be uninitialised.

If you want a copy then do a new malloc and use memcpy.

Implementation-wise, when you call realloc to increase the size, one of these things might happen:

  • A new block is allocated and the contents of the old memory copied, the old block is freed, the new pointer is returned.
  • If the area after the block is not allocated, the existing block may be extended and the same pointer returned.

Since you have no way of knowing which has happened, or even if a completely different implementation to that suggested above is used, you should always code according to the spec of realloc, which is that you must not use the old pointer any more and you must use the new one.


It's hard to tell what you're asking, but if you're asking whether you can read the "old contents" at the old address passed to realloc, the answer is no. In some cases, you may find part or all of the old contents there, but unless realloc returned the same pointer you passed to it, any use of the old pointer is undefined behavior.

If you're merely asking whether the old contents will be preserved at the new address returned by realloc, the answer is yes (up to the minimum of the old size and the new size).