Home  >  Article  >  Backend Development  >  Briefly describe what is the function of allocator?

Briefly describe what is the function of allocator?

青灯夜游
青灯夜游Original
2020-07-15 14:58:346651browse

The allocator is an important part of the C standard library. Its function is to handle the container's memory allocation and release requests. In other words, the allocator is used to encapsulate the low-level details of memory management of STL containers.

Briefly describe what is the function of allocator?

The role of the allocator

In C programming, the allocator (English: allocator) is the C standard library important parts of.

C's library defines a variety of data structures (such as linked lists, sets, etc.) collectively called "containers". One of the common features of these containers is that their size can be changed while the program is running; In order to achieve this, dynamic memory allocation is particularly necessary, where the allocator is used to handle the container's memory allocation and release requests.

In other words, the allocator is used to encapsulate the low-level details of memory management of the STL container.

By default, the C standard library uses its own universal allocator, but according to specific needs, programmers can also customize their own allocator to replace it.

The allocator was first invented by Alexander Stepanov as part of the C Standard Template Library (STL). Its original intention was to create a method that "makes the library more flexible and independent "based on the underlying data model" and allows programmers to utilize custom pointers and reference types in the library;

But when the standard template library was incorporated into the C standard, the C standards committee realized that the data model was completely As a compromise, the allocator restrictions in the standard have become more stringent, resulting in unacceptable performance losses due to abstraction. As a result, the existing standard is significantly less efficient than Stepanov's original vision. The dispensers described are significantly limited in how customizable they are.

Although the customization of allocators is limited, in many cases, custom allocators are still needed, and this is usually to encapsulate different types of memory spaces (such as shared memory and reclaimed memory). ) access method, or to improve performance when using a memory pool for memory allocation. In addition, from the perspective of memory usage and running time, programs that frequently allocate small amounts of memory will benefit a lot from introducing a specially customized allocator.

Recommended tutorial: "c Video Tutorial"

The above is the detailed content of Briefly describe what is the function of allocator?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn