C ++ allocator

Source: Internet
Author: User

Recently, I heard from my colleagues that a company asked C ++ allocator related questions during an interview.

Allocator templates are essentially a set of memory management requirements of stl containers. They are called concept in generic programming. When you create allocator by yourself, there is no problem as long as you meet these requirements. It is worth noting that a very interesting structure in allocator has to be mentioned. This is rebind. Its purpose is to get another specific allocator class through a specific allocator class, this is the programming of class and can be considered as a simple template meta programming. For example, allocator <int >:: rebind <long >:: other is a allocator <long>.

Rebind mainly serves stl chain containers, such as list. When the list template is embodied as a class, you can specify the element type of the list and the memory allocation policy of the elements (such as list <int, myallocator <int> ), however, the memory allocation policy of the list node cannot be specified. In fact, the memory allocation policy of the list node should be the same as that of the element. In this case, rebind is required to generate allocator for the list node.

In the end, the current allocator can only be used for memory allocation management, but cannot be used for address model adaptation. I don't know if it will change in the future.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.