Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should C++ allocator::allocate throw or return nullptr when allocation fails?

Tags:

c++

std

allocator

The Allocator concept and std::allocator_traits do not say what allocate will do when allocation fail -- will it returns nullptr or throw?

When I'm writing a container using standard allocator API, should I

  1. Check the return value and catch the exception in the noexcept version member function(E.g. push_back, resize...);

  2. Check the return value and throw if fail in the exception-throwing one

so that no matter it throws or not, I will get the correct behavior.

like image 267
JiaHao Xu Avatar asked May 14 '18 09:05

JiaHao Xu


People also ask

Which of the given member function S is are associated with std :: allocator ()?

Member functions associated with std::allocator() : address: It is used for obtaining the address of an object although it is removed in C++20. construct: It is used to construct an object.It is also removed in C++20. destroy: It is used to destruct an object in allocated storage.It is also removed in C++20.

What does allocator mean in C++?

Allocators are used by the C++ Standard Library to handle the allocation and deallocation of elements stored in containers. All C++ Standard Library containers except std::array have a template parameter of type allocator<Type> , where Type represents the type of the container element.

What is container allocator?

In C++ computer programming, allocators are a component of the C++ Standard Library. The standard library provides several data structures, such as list and set, commonly referred to as containers. A common trait among these containers is their ability to change size during the execution of the program.

What is allocator implemented?

Allocators represent a special memory model and are an abstraction used to translate the need to use memory into a raw call for memory. They provide an interface to allocate, create, destroy, and deallocate objects. With allocators, containers and algorithms can be parameterized by the way the elements are stored.


1 Answers

Draft n4659 for C++ standard says at 23.10.9 The default allocator [default.allocator] (emphasize mine):

23.10.9.1 allocator members [allocator.members]
...

T* allocate(size_t n);

2 Returns: A pointer to the initial element of an array of storage of size n * sizeof(T), aligned appropriately for objects of type T.
3 Remarks: the storage is obtained by calling ::operator new (21.6.2), but it is unspecified when or how often this function is called.
4 Throws: bad_alloc if the storage cannot be obtained.

It makes it clear that the standard allocator will raise a bad_alloc exception if it cannot allocate storage.


Above is for the standard allocator. The requirement for any allocator are described in 20.5.3.5 Allocator requirements [allocator.requirements] and table 31 — Allocator requirements contains:

a.allocate(n) [Return type:] X::pointer [Assertion/note/ pre-/post-condition]Memory is allocated for n objects of type T but objects are not constructed. allocate may throw an appropriate exception

My understanding is that allocate can return only when memory has been allocated. So the allocator should throw an appropriate exception (not necessarily bad_alloc even if it would be quite appropriate) if memory could not be allocated.

like image 125
Serge Ballesta Avatar answered Nov 15 '22 06:11

Serge Ballesta