九、 通用工具 ----动态内存管理--allocator

xiaoxiao2021-02-28  55

1 动态内存管理

头文件<memory>提供了内存管理;

1 默认配置器

一些和内存管理相关的模板类和函数, - allocator : 模板类 - raw_storage_iterator: 内存管理的迭代器 - get_temporary_buffer(): 返回临时缓冲区的指针; - return_temporary_buffer():释放掉临时缓冲区; - uninitialized_copy(): 从指定的源区域复制对象到未初始化的目标范围; - uninitialized_copy_n: 上面类似,只是有n个数值的限制; - uninitialized_fill():根据数据类型对数据进行数值填充; - uninitialized_fill_n():根据数据类型对数据进行数值填充n个;

1.1 allocator

定义:

template< class T > struct allocator; template<> struct allocator<void>;

1.1.1 allocator构造函数

//default (1) allocator() noexcept; allocator (const allocator& alloc) noexcept; //copy (2) template <class U> allocator (const allocator<U>& alloc) noexcept;

1.1.2 allocator的成员函数

address()

pointer address ( reference x ) const noexcept; const_pointer address ( const_reference x ) const noexcept;

说明: 返回x的地址。 在标准默认分配器中,这意味着返回&x。

allocate

pointer allocate (size_type n, allocator<void>::const_pointer hint=0);

说明:

n: 要分配的元素数(每个大小sizeof(value_type))。hint: 默认为0,被用来改善实现的性能,如果不为0,该值可以用作通过在指定的附近分配新块来提高性能的提示;返回值:指向存储块中初始元素的指针。

deallocate

void deallocate (pointer p, size_type n);

说明:

释放先前分配给成员分配但尚未发布的存储块。数组中的元素不会被对该成员函数的调用所破坏。p: 指向内存的指针n:从p开始截取n个元素;

max_size

size_type max_size() const noexcept;

说明: 返回allocate开辟内存指定的内存大小;

construct

template <class U, class... Args> void construct (U* p, Args&&... args);

说明: 在p指向的位置构造一个元素对象。使用不定的参数进行初始化;

destroy

template <class U> void destroy (U* p);

说明: 1. 销毁p所指向的对象。请注意,这不会释放元素的存储; 2. 该函数使用U的析构函数,就好像使用了以下代码: p->~U()


1.2 raw_storage_iterator

原型:

template <class OutputIterator, class T> class raw_storage_iterator : public iterator<output_iterator_tag,void,void,void,void> { protected: OutputIterator iter_; public: explicit raw_storage_iterator (OutputIterator x) : iter_(x) {} raw_storage_iterator<OutputIterator,T>& operator* () { return *this; } raw_storage_iterator<OutputIterator,T>& operator= (const T& element) { new (static_cast<void*>(&*iter_)) T (element); return *this; } raw_storage_iterator<OutputIterator,T>& operator++ () { ++iter_; return *this; } raw_storage_iterator<OutputIterator,T> operator++ (int) { raw_storage_iterator<OutputIterator,T> tmp = *this; ++iter_; return tmp; } };

说明:

使用参数x初始化迭代器的值;*操作符 返回元素;赋值运算符= 使用参数构造一个值,返回迭代器的引用;++操作符使迭代器前进,返回的引用有区别;

例子:

#include <iostream> #include <string> #include <memory> #include <algorithm> int main() { const std::string s[] = {"This", "is", "a", "test", "."}; std::string* p = std::get_temporary_buffer<std::string>(5).first; std::copy(std::begin(s), std::end(s), std::raw_storage_iterator<std::string*, std::string>(p)); for(std::string* i = p; i!=p+5; ++i) { std::cout << *i << '\n'; i->~basic_string<char>(); } std::return_temporary_buffer(p); }

输出为:

This is a test .

1.3 临时缓冲区

1.3.1 get_temporary_buffer

template <class T> pair <T*,ptrdiff_t> get_temporary_buffer ( ptrdiff_t n ) noexcept;

说明: 1. 获取临时缓冲区的指针,该缓冲区必须存储n个相邻的元素; 2. 返回一个pair对象,first为指向缓冲区的指针,second为缓冲区容量;

1.3.2 return_temporary_buffer

template <class T> void return_temporary_buffer (T* p);

说明:

释放p指向的缓冲区;缓冲区应该是最初由get_temporary_buffer分配的内存;

1.4 特定算法

1.4.1 uninitialized_copy

template <class InputIterator, class ForwardIterator> ForwardIterator uninitialized_copy ( InputIterator first, InputIterator last, ForwardIterator result );

说明: 范围[first,last)内的元素复制到一个未初始化的内存区开始在result。使用拷贝构造函数拷贝元素到未初始化的区域.

1.4.2 uninitialized_copy_n

template< class InputIt, class Size, class ForwardIt > ForwardIt uninitialized_copy_n( InputIt first, Size count, ForwardIt d_first);

说明: 范围内[first,first+count]的元素复制到一个未初始化的内存区开始在result。使用拷贝构造函数拷贝元素到未初始化的区域.

1.4.3 uninitialized_fill

template< class ForwardIt, class T > void uninitialized_fill( ForwardIt first, ForwardIt last, const T& value )

说明: 复制给定值value到一个未初始化的范围[first, last)内存区域;

1.4.4 uninitialized_fill_n

template< class ForwardIt, class Size, class T > void uninitialized_fill_n( ForwardIt first, Size count, const T& value )

说明: 将first开始的区域填充count个value值;

转载请注明原文地址: https://www.6miu.com/read-53412.html

最新回复(0)