Allocating zero objects with std::allocator::allocate(0) in C++
Asked Answered
C

2

6

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?

Calutron answered 4/12, 2018 at 10:3 Comment(0)
L
4

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."

Lananna answered 4/12, 2018 at 10:17 Comment(2)
I suspect that's related to the implementation of (if not directly inherited from) C's 7.22.3 Memory management functions: "If the size of the space requested is zero, the behavior is implementation-defined: either a null pointer is returned, or the behavior is as if the size were some nonzero value, except that the returned pointer shall not be used to access an object."Domestic
@AndrewHenle yes but it is not allowed to return nullptr like C isSculpt
D
3

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.

Denaedenarius answered 4/12, 2018 at 10:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.