C++11标准模板(STL)- 动态内存管理 - 获得未初始化存储 - (std::get_temporary_buffer)

动态内存管理

初始化存储

提供数个工具以创建并访问未初始化存储

获得未初始化存储

std::get_temporary_buffer

template< class T >
std::pair< T*, std::ptrdiff_t > get_temporary_buffer( std::ptrdiff_t count );

(C++17 中弃用)
(C++20 中移除)

分配未初始化的相接存储,它应足以存储至多 countT 类型的相邻对象。要求是非绑定的,且实现可以分配少于或多于存储 count 个相邻对象所需的内存。

参数

count-所需的对象数量

返回值

一个 std::pair ,保有指向被分配存储起始的指针,和适合在实际分配的存储中的对象数。

若无法分配内存,或若分配的存储不足以存储单个 T 类型元素,则结果的 first 元素为空指针且 second 元素为零。

异常

(无)

(C++11 前)
noexcept 规定:  

noexcept

  
(C++11 起)

调用示例

#include <iostream>
#include <string>
#include <iterator>
#include <algorithm>
#include <functional>
#include <time.h>
#include <random>
#include <vector>
#include <cassert>
#include <memory>

struct Cell
{
    int x;
    int y;

    Cell(): x(0), y(0)
    {
    }

    Cell(int a, int b): x(a), y(b) {}

    Cell &operator +=(const Cell &cell)
    {
        x += cell.x;
        y += cell.y;
        return *this;
    }

    Cell &operator +(const Cell &cell)
    {
        x += cell.x;
        y += cell.y;
        return *this;
    }

    Cell &operator *(const Cell &cell)
    {
        x *= cell.x;
        y *= cell.y;
        return *this;
    }

    Cell &operator ++()
    {
        x += 1;
        y += 1;
        return *this;
    }


    bool operator <(const Cell &cell) const
    {
        if (x == cell.x)
        {
            return y < cell.y;
        }
        else
        {
            return x < cell.x;
        }
    }

    bool operator >(const Cell &cell) const
    {
        if (x == cell.x)
        {
            return y > cell.y;
        }
        else
        {
            return x > cell.x;
        }
    }

    bool operator ==(const Cell &cell) const
    {
        return x == cell.x && y == cell.y;
    }

    friend Cell operator+(const Cell &lcell, const Cell &rcell)
    {
        Cell cell = lcell;
        cell.x += rcell.x;
        cell.y += rcell.y;
        return cell;
    }

    friend Cell operator-(const Cell &lcell, const Cell &rcell)
    {
        Cell cell = lcell;
        cell.x -= rcell.x;
        cell.y -= rcell.y;
        return cell;
    }

    friend Cell operator*(const Cell &lcell, const Cell &rcell)
    {
        Cell cell = lcell;
        cell.x *= rcell.x;
        cell.y *= rcell.y;
        return cell;
    }

    friend Cell operator/(const Cell &lcell, const Cell &rcell)
    {
        Cell cell = lcell;
        cell.x /= rcell.x;
        cell.y /= rcell.y;
        return cell;
    }

    friend Cell operator%(const Cell &lcell, const Cell &rcell)
    {
        Cell cell = lcell;
        cell.x %= rcell.x;
        cell.y %= rcell.y;
        return cell;
    }
};

std::ostream &operator<<(std::ostream &os, const Cell &cell)
{
    os << "{" << cell.x << "," << cell.y << "}";
    return os;
}

int main()
{
    std::cout << "std::get_temporary_buffer" << std::endl;
    std::cout << std::boolalpha;

    const std::string strings[] = {"This", "is", "a", "test", "."};
    std::string* pStrings = std::allocator<std::string>().allocate(5);

    std::pair<std::string*, ptrdiff_t> get_temporary_buffer_string
        = std::get_temporary_buffer<std::string>(4);
    // 要求将 p.first 传递给 return_temporary_buffer
    // (当心提前退出和异常)
    std::copy(strings, strings + get_temporary_buffer_string.second,
              std::raw_storage_iterator<std::string*, std::string>(get_temporary_buffer_string.first));
    // 要求单独销毁每个 p 中的 string
    // (当心提前退出和异常)

    std::copy(get_temporary_buffer_string.first,
              get_temporary_buffer_string.first + get_temporary_buffer_string.second,
              std::ostream_iterator<std::string> {std::cout, "\n"});

    std::for_each(get_temporary_buffer_string.first,
                  get_temporary_buffer_string.first + get_temporary_buffer_string.second,
                  [](std::string & string)
    {
        string.~basic_string<char>();
    });


    std::mt19937 g{std::random_device{}()};
    srand((unsigned)time(NULL));

    auto generate = []()
    {
        int n = std::rand() % 10 + 110;
        Cell cell{n, n};
        return cell;
    };

    //3) 构造拥有 count 个有值 value 的元素的容器。
    const Cell cells[] = {generate(), generate(), generate(), generate(), generate()};
    std::cout << "Cell cells:  ";
    std::copy(std::begin(cells), std::end(cells), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;

    std::pair<Cell*, ptrdiff_t> get_temporary_buffer_Cell
        = std::get_temporary_buffer<Cell>(6);
    // 要求将 p.first 传递给 return_temporary_buffer
    // (当心提前退出和异常)
    std::copy(cells, cells + get_temporary_buffer_Cell.second,
              std::raw_storage_iterator<Cell*, Cell>(get_temporary_buffer_Cell.first));

    std::cout << "Cell pCells:  ";
    std::copy(get_temporary_buffer_Cell.first,
              get_temporary_buffer_Cell.first + get_temporary_buffer_Cell.second,
              std::ostream_iterator<Cell> {std::cout, "\n"});

    std::for_each(get_temporary_buffer_Cell.first,
                  get_temporary_buffer_Cell.first + get_temporary_buffer_Cell.second,
                  [](Cell & cell)
    {
        cell.~Cell();
    });
    //创建新的 get_temporary_buffer
    std::pair<Cell*, ptrdiff_t> get_temporary_buffer_Cell1
        = std::get_temporary_buffer<Cell>(6);
    std::cout << "std::get_temporary_buffer<Cell>(6):   "
              << typeid(get_temporary_buffer_Cell1).name() << std::endl;
    std::cout << "std::get_temporary_buffer<Cell>(6).first:   "
              << typeid(get_temporary_buffer_Cell1.first).name() << std::endl;
    std::cout << "std::get_temporary_buffer<Cell>(6).second:   "
              << typeid(get_temporary_buffer_Cell1.second).name() << std::endl;

    std::allocator<std::string>().deallocate(pStrings, 5);
    std::cout << std::endl;


    std::vector<Cell> vector1(8, generate());
    std::generate(vector1.begin(), vector1.end(), generate);
    std::cout << "vector1:  ";
    std::copy(vector1.begin(), vector1.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;
    std::cout << std::endl;

    return 0;
}

输出

std::get_temporary_buffer
This
is
a
test
Cell cells:  {118,118} {118,118} {117,117} {110,110} {111,111}
Cell pCells:  {118,118}
{118,118}
{117,117}
{110,110}
{111,111}
{127,-795755684}
std::get_temporary_buffer<Cell>(6):   St4pairIP4CelliE
std::get_temporary_buffer<Cell>(6).first:   P4Cell
std::get_temporary_buffer<Cell>(6).second:   i

vector1:  {110,110} {112,112} {114,114} {116,116} {116,116} {119,119} {118,118} {112,112}

  • 5
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值