tags:

views:

109

answers:

4

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?

+5  A: 

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.

ChrisW
And the pointer is also made to point to the new location?
fahad
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);`
ChrisW
@ChrisW: if the realloc fails, your `ptr` points to NULL and the old ptr gets lost (read `memory leak`)
pmg
@pmg You're right. I just did +1 to [your answer](http://stackoverflow.com/questions/3850749/does-realloc-overwrites-old-contents/3850798#3850798) which is a more careful version of overwriting the existing pointer.
ChrisW
+1  A: 

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

R..
A: 

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.

jhabbott
+8  A: 

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 */
pmg