Skip to content

Std::allocator

할당자는 C++의 표준 라이브러리에서 메모리 할당을 관리하는 객체이다. C++표준 라이브러리에서 이용되는 리스트, 셋 등의 다양한 컨테이너 자료구조들은 용도에 따라 동적 메모리 할당을 요구한다. 할당자는 동적 메모리 할당을 통합 관리하는데 필요한 기능이 정의된 객체이다.

Category

Example

#include <memory>
#include <iostream>
#include <string>

int main()
{
    std::allocator<int> a1; // default allocator for ints
    int* a = a1.allocate(10); // space for 10 ints

    a[9] = 7;

    std::cout << a[9] << '\n';

    a1.deallocate(a, 10);

    // default allocator for strings
    std::allocator<std::string> a2;

    // same, but obtained by rebinding from the type of a1
    decltype(a1)::rebind<std::string>::other a2_1;

    // same, but obtained by rebinding from the type of a1 via allocator_traits
    std::allocator_traits<decltype(a1)>::rebind_alloc<std::string> a2_2;

    std::string* s = a2.allocate(2); // space for 2 strings

    a2.construct(s, "foo");
    a2.construct(s + 1, "bar");

    std::cout << s[0] << ' ' << s[1] << '\n';

    a2.destroy(s);
    a2.destroy(s + 1);
    a2.deallocate(s, 2);
}

Allocator library

See also

Favorite site

References


  1. Cpp_Standard_Library_-infobook-_Allocator.pdf 

  2. Stl_allocator_and_rebind.pdf 

  3. Ohyecloudy.com_-EASTL-_allocator.pdf