Are there any guarantees that realloc() will always shrink a buffer in-place?? So that the following:
new_ptr = (data_type *) realloc(old_ptr, new_size * sizeof(data_type));
will always give new_ptr == old_ptr if new_size < old_size (except of course when new_size == 0). It seems sensible (to me) that it would work this way, but was curious whether the standard enforced it.
I’m looking at the reallocation of arrays of non-POD data types, and if the above behaviour was guaranteed was thinking that the following strategy might at least allow efficient “shrinking”:
if (new_size > old_size)
{
// malloc() a new buffer
// use placement copy constructor to copy old objects over
// free() old buffer
}
else
if (new_size < old_size)
{
// explicit destruction of unneeded objects
// realloc() buffer
}
I”m expecting that an in-place “shrink” would be robust even if the data type had self references/pointers or whatever…
No.
That’s it. None of this "it may work in some architectures" or "it should, based on experience". The standard states clearly that the address may change so rely on that and nothing more. In any case, you asked if it was guaranteed – the answer that is a definite no(a).
In terms of coding to the standard: do, or do not. There is no "try" 🙂
From c99:
(a) If you’re wondering why you wouldn’t just split up a buffer into two smaller buffers (keeping one and returning the other to the free list) for efficiency, there is at least one possibility that springs to mind.
If you have different pools for allocations of different sizes (which may use different allocation strategies, for example), it might make sense to move the data over to the pool for smaller allocations. The efficiency gains you get from separate pools may outweigh the gains of leaving memory in place.
But that’s just an example, I have no idea whether any implementation does that. As stated, you should rely on what the standard mandates, which is that the memory may move even when shrinking.