new int[0]
is permitted in C++ but is std::allocator<int>().allocate(0)
well defined?
More general, must all Allocators accept 0
as a parameter to allocate?
Edit:
After reading the answers I tested Visual Studio's std::allocator
: allocate(0)
gives nullptr
deallocate(nullptr, anything)
is a nop.
So using nullptr
is a good suggestion, but the standard do not demand that deallocate(nullptr, 0)
is a nop, see C++ allocator::deallocate(NULL,1) allowed?
Table 34 — Cpp17Allocator requirements
Memory is allocated for n objects of type T but objects are not constructed. allocate may throw an appropriate exception.174 [Note: If n == 0, the return value is unspecified. —end note]
I would read that as "The allocator shall/should handle n == 0
, not throw and return a value that might be a valid pointer or be nullptr
."
Indeed new int[0]
is well-defined. Note that you are required to call delete[]
on the returned pointer, and the behavior on dereferencing the pointer is undefined.
Similar rules apply to std::allocator().allocate(0)
: you can't dereference the returned pointer, and you need to clean up memory in the normal way by calling std::allocator::deallocate
.
An allocator is allowed to throw a std::bad_alloc
exception; you could deal with the 0 parameter case by doing that, without contravening any requirements laid down by the standard. Returning nullptr
is an alternative.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With