Does realloc actually shrink buffers in common implementations?

I can say about Linux/glibc. In source code it contains comments like this: if n is for fewer bytes than already held by p, the newly unused space is lopped off and freed if possible if you look at code of glibc, it contains lines like this: remainder_size = newsize - nb; if (remainder_size > 1)) { /* * Serious shrinkage (more than half). Free() the excess.

*/ return tiny_try_shrink_in_place(szone, ptr, old_size, new_good_size); } else if (new_good_size.

I can say about Linux/glibc. In source code it contains comments like this: if n is for fewer bytes than already held by p, the newly unused space is lopped off and freed if possible. If you look at code of glibc, it contains lines like this: remainder_size = newsize - nb; if (remainder_size > 1)) { /* * Serious shrinkage (more than half).Free() the excess.

*/ return tiny_try_shrink_in_place(szone, ptr, old_size, new_good_size); } else if (new_good_size.

Whether or not its worth it depends on how long the object is going to be around and how important it is to the application to reduce its memory footprint. There's no one right generic answer. Generic memory allocators typically assume that the caller knows the previous size of the block and would only be called realloc if they actually knew they wanted to shrink the block.

The last one I looked at was willing to shrink the block if the block was already over 128 bytes and the reallocation would free at least 1KB or at least a number of bytes equal to 1/4 of the block's current allocation size. It was tuned for high-volume server applications where objects typically don't stay around very long and where a special 'right size' operation was offered for objects known to be around for very long periods of time.

1 +1 for covering possible good strategies. – R.. Nov 18 at 2:33.

Yes, they do. But standard says nothing that forces that. So you have to check it with the libc you are targeting.

You can do that either by looking at the code, if available, or by writing a test program. The idea for the test program is this - you allocate relatively large block (say 10K), then try to shrink like a half with realloc and allocate something small with malloc. If newly returned address lays in the range you have allocated for the first time then your realloc shrinks, otherwise it doesn't.

If newly returned address lays in the range you have allocated for the first time then your realloc shrinks, otherwise it may or may not. There might be reasons for the second malloc to allocate somewhere other than the previously shrunk region, even if it's available. – Keith Thompson 2 days ago.

The realloc() function returns a pointer to the newly allocated memory, which is suitably aligned for any kind of variable and may be different from ptr, or NULL if the request fails. If size was equal to 0, either NULL or a pointer suitable to be passed to free() is returned. If realloc() fails the original block is left untouched; it is not freed or moved.

I can read the manpage myself, thank you. – larsmans 2 days ago @larsmans, how would he know that?! :-D – Amigable Clark Kant yesterday.

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