Does realloc overwrite old contents?
Asked Answered
A

4

42

When we reallocate memory via realloc(), are the previous contents over-written? I am trying to make a program which reallocates memory each time we enter the data into it.

Please tell me about memory allocation via realloc, is it compiler dependent for example?

Alphabetize answered 3/10, 2010 at 17:18 Comment(0)
F
96

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 */
Frymire answered 3/10, 2010 at 17:32 Comment(5)
After oldpointer = newpointer; should newpointer be freed or I must keep it?Indiraindirect
After oldpointer = newpointer; both pointers point to the same area of memory. Doing free(newpointer); would be the same as free(oldpointer);. You do not want to mess with newpointer in any way: just forget it exists.Frymire
Let's assume that oldpointer points to a memory block with address: 0xaaaaaa on doing this: int *newpointer = realloc(oldpointer, 1000); , realloc will try to allocate memory to the same block if possible, if not it will search for another memory block (of course bigger than the old one). Say the new block is at address: 0xbbbbbb. Now on successful reallocation of memory what happens to address: 0xaaaaaa and its contents. Does realloc calls free() on the old memory block on successful reallocation of the memory?Griswold
@barnes: yes, when realloc() "works" it automatically does the equivalent of calling free() on the old memory block; see C11 7.22.3.5 "The realloc function deallocates the old object ...".Frymire
The document says: The content of the memory block is preserved up to the lesser of the new and old sizes, even if the block is moved to a new location. And I think realloc may be faster than malloc+memcpy if the return address is the same with old pointer address.Bourbon
O
16

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.

Oballa answered 3/10, 2010 at 17:20 Comment(3)
realloc returns a new pointer value, whose value may or may not be the same as the old pointer value, and which you should use to overwrite your previous pointer value: e.g. ptr = realloc(ptr, new_size);Oballa
if the realloc fails, your ptr points to NULL and the old ptr gets lost (read memory leak)Frymire
@Frymire You're right. I just did +1 to your answer which is a more careful version of overwriting the existing pointer.Oballa
T
10

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.

Triplex answered 3/10, 2010 at 17:25 Comment(2)
Hello 10 years later) You said: "A new block is allocated and the contents of the old memory copied, the old block is freed, the new pointer is returned". So, if I use the same pointers: p = realloc(p, ...), does it mean that the old memory will be freed? Will I lose my data?Fontainebleau
No, the data will be copied for you into the new block that the returned p points at, before the old block is freed. This all happens before realloc returns, so the new p points to your data still.Triplex
E
6

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).

Entree answered 3/10, 2010 at 17:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.