Hey guys,

I was wondering if realloc() copied the contents of the block of memory to the new block "on most compilers" IF the new size is bigger. I like to learn good ways to program something, but don't want to reinvent realloc just because it doesn't work on <reallyoldcompiler>.

Thanks in advance,

Recommended Answers

All 7 Replies

So what is your doubt anyway... :D

"I was wondering if realloc() copied the contents of the block of memory to the new block"... The answer is NO... realloc only increases the size of the previously allocated memory...

In no implementation it copies if the size is bigger?

My doubt is that it DOES copy when the size is smaller, so it seemed logical it also does that when the size increases, but the it's undefined behavior.

I have not encountered a situation in which it copies even if the size is smaller...

> but don't want to reinvent realloc just because it doesn't work on <reallyoldcompiler>.
Care to post an example of that which "doesn't work".
Because realloc is a tricky function, and odds are that it's your code that screwed up.

Oh, and there's absolutely no guarantee that the memory will (or won't) move on every single call, whether you're making it bigger, smaller or the same size.
Some debug versions move it all the time just to make sure the caller is being honest.

>Oh, and there's absolutely no guarantee that the memory will (or won't) move on every single call, whether you're making it bigger, smaller or the same size.
-
I don't know when it "doesn't work", that's exactly what I'm asking. With "doesn't work" I meant that it doesn't copy.

I figured it'd copy from reading this:
"The realloc() function changes the size of the memory object pointed to by ptr to the size specified by size. The contents of the object will remain unchanged up to the lesser of the new and old sizes. If the new size of the memory object would require movement of the object, the space for the previous instantiation of the object is freed. If the new size is larger, the contents of the newly allocated portion of the object are unspecified. If size is 0 and ptr is not a null pointer, the object pointed to is freed. If the space cannot be allocated, the object remains unchanged."

Does that say that the contents of the memory pointed to will be the same after the call to realloc() when the size is smaller or equal? Or is it nothing of that?

It says

If the space cannot be allocated, the object remains unchanged.

If that was your question?

Furthermore, read this, I think it should help you: realloc() info

Bottom point is, you can easily determine if memory has been copied to another place:

//ptr is already allocated with malloc or calloc
Type *temp;
int new_size = 1000*sizeof(Type);

temp = realloc(ptr, new_size);
if (temp == ptr)  
       printf ("Memory hasn't been moved\n");
else{
       printf ("Memory has been moved to another location\n"); 
       ptr = temp;
}
//do your buisness with ptr

If it moves, and is smaller, you get a smaller copy.

But (and this is a biggie, because it's a bug in your code), you do this.

p = realloc( p, some_size );
// do stuff upto some_size
p = realloc( p, some_smaller_size );  
// BADNESS
// do stuff with memory past some_smaller_size

Now, if it doesn't move when the size shrinks, you could well get lucky and still see your old data. But your code is broken.
Some time later, making the memory smaller also moves it, and then you're in a completely new place reading garbage.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.