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

动态内存管理

初始化存储

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

释放未初始化存储

std::return_temporary_buffer

定义于头文件 <memory>

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

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

解分配先前以 std::get_temporary_buffer 分配的存储。

参数

p-指向先前由 std::get_temporary_buffer 分配的存储,且未被之前到 return_temporary_buffer 的调用非法化的指针

返回值

(无)

异常

(无)

(C++17 起)

调用示例

#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::return_temporary_buffer(get_temporary_buffer_string.first);

    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::return_temporary_buffer(get_temporary_buffer_Cell1.first);

    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:  {113,113} {117,117} {113,113} {117,117} {117,117}
Cell pCells:  {113,113}
{117,117}
{113,113}
{117,117}
{117,117}
{406,-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:  {111,111} {110,110} {111,111} {114,114} {113,113} {117,117} {119,119} {111,111}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值