Can realloc move pointer if new size smaller?

opengroup.org/onlinepubs/007908775/xsh/r... Upon successful completion with a size not equal to 0, realloc() returns a pointer to the (possibly moved) allocated space Nope, no guarantee.

opengroup.org/onlinepubs/007908775/xsh/r... Upon successful completion with a size not equal to 0, realloc() returns a pointer to the (possibly moved) allocated space. Nope, no guarantee.

Earlier on that page, it says "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."

It doesn't rule out motion, but it is relatively unlikely. – Jonathan Leffler Nov 15 '09 at 5:30 Yes, you are still guaranteed that whatever was in memory before would still be there, thanks for pointing that out – Jeffrey Aylesworth Nov 15 '09 at 15:19.

There's no guarantee realloc will return the same location, period.

With realloc, you get absolutely no guarantees about where the memory will live afterwords. I believe that libc's default malloc will only begrudgingly copy memory around, so practically speaking you may be OK. But don't count on it.

Realloc is not required to leave the block in place even if it would fit, and in fact the simplest stub implementation is an example where it might not: malloc: call sbrk. Realloc: call malloc and memcpy. Free: no-op.

This may sound ridiculous, but sometimes for embedded systems an implementation like I've just described is actually the optimal one.

Another example is an implementation where all adjacent allocations are blocks of the same size in order to avoid fragmentation. In that case, a 32-byte block no longer belongs in the same location as the former 4096-byte block. – Zan Lynx May 11 at 21:01 Yes.

Another more advanced example would be an implementation which examines whether the left-hand neighbor of the block to be shrunk is free, whether a significant free block will be created on the right-hand side by shrinking, whether the resulting size is "small enough" that memcpy is not too expensive... and if the right conditions are met, moves the the block to a new location to avoid fragmentation. – R.. May 11 at 22:41.

On Windows, the C-Runtime grabs a heap, and then allocates memory from that heap. So the OS won't know about individual memory allocations, and thus won't move things around.

1 This is not correct. The Visual C run-time does not directly call the OS heap implementation, for one thing. For another, the HeapReAlloc() call does move things around.

– Heath Hunnicutt Nov 15 '09 at 3:18 You need to double check your docs. See: msdn.microsoft. Com/en-us/library/csd157zx.

Aspx The CRT grabs a single OS heap to use internally. It then sub-allocates that heap (meaning it doesn't use the Win32 heap calls to do allocations within that heap) – DougN Nov 16 '09 at 14:54.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions