Consider this example,
// x is a int* allocated previously via 'new'
delete x;
x = new int;
Does this code always guarantee a memory allocation in last line as there is definitely memory left to hold an int at least from the previous deallocation.
In case the standard currently does not provide any such guarantee, is that decision because of a room for a potential optimization?
Does any popular implementations get advantage of that optimization or do all of them simply guarantees the allocation after deallocation?
Or is it just impossible to provide such guarantee? In that case, can you provide a case where the allocation after the deallocation is absolutely bound to fail.
When I say allocation after deallocation in the above question, I'm talking about allocation of equal or smaller size than deallocation as I know allocating a larger size may not always be possible due to insufficient memory.
There is no guarantee that you'll get a new memory location, in fact the standard doesn't provide any details about how memory allocations work,
newreturns a pointer to a new object, that's about all the standard says. In some implementations you might even find that your code always produces the same object and there's nothing illegal about that according to the standard.https://en.cppreference.com/w/cpp/language/new
In places where the behaviour of the language isn't important to the user the standard is deliberately unspecified in order to not restrict implementers. For example in the case of
newthe memory allocation could be implemented using a heap, using a fixed size pool of pre-allocated memory or it could even have a separate pool of memory blocks for each object size.