C++标准模板(STL)- C 内存管理库 - 解分配之前分配的内存 (std::free)

C 内存管理库

解分配之前分配的内存

std::free

void free( void* ptr );

解分配先前由 std::malloc() 、 std::calloc() 、 std::aligned_alloc (C++17 起) 或 std::realloc() 分配的内存空间。

ptr 是空指针,则函数不做任何事。

ptr 的值不等于先前 std::malloc() 、 std::calloc() 、 std::aligned_alloc (C++17 起) 或 std::realloc() 返回的值,则行为未定义。

ptr 所指代的内存区域已被解分配,即已以 ptr 为参数掉调用 std::free() 或 std::realloc() ,且无对 std::malloc() 、 std::calloc() 、 std::aligned_alloc (C++17 起) 或 std::realloc() 产生等于之前 ptr 的指针,则行为未定义。

若在 std::free() 返回后,通过指针 ptr 访问(除非另一分配函数恰好产生等于 ptr 的指针值),则行为未定义。

要求下列函数是线程安全的:

  • operator new 及 operator delete 的库版本
  • 全局 operator new 与 operator delete 的用户替换版本
  • std::calloc 、 std::malloc 、 std::realloc 、 std::aligned_alloc (C++17 起) 、 std::free

对这些分配或解分配特定存储单元的函数调用以单独全序出现,并且在此顺序中,每个解分配调用先发生于下个分配(若存在)。

(C++11 起)

参数

ptr-指向要解分配的内存的指针

返回值

(无)

注意

此函数接受空指针(不做任何事)以减少特殊情况的总数。无论分配是否成功,分配函数返回的指针都能传递给 free()

调用示例

#include <iostream>
#include <cstdlib>
#include <string>

class MyString : public std::string
{
public:
    MyString() : std::string()
    {
        std::cout << __FUNCTION__ << std::endl;
    }

    MyString(size_type count, char ch)
        : std::string(count, ch)
    {
        std::cout << __FUNCTION__ << "  "
                  << static_cast<void *>(this) << std::endl;
    }

    ~MyString()
    {
        this->~basic_string();
        std::cout << __FUNCTION__ << "  "
                  << static_cast<void *>(this) << std::endl;
    }
};

int main()
{
    // 为 4 个 string 的数组分配足够空间
    if (auto point = (MyString*)std::malloc(5 * sizeof(MyString)))
    {
        int i = 0;
        try
        {
            for (; i != 5; ++i) // 填充数组
            {
                new (point + i) MyString(5, 'a' + i);
            }

            for (int j = 0; j != 5; ++j) // 打印出来
            {
                std::cout << "point[" << j << "] == " << point[j] << std::endl;
            }
        }
        catch (...) {}

        for (; i != 0; --i) // 清理
        {
            point[i - 1].~MyString();
        }

        std::free(point);
    }

    std::cout << std::endl << std::endl << std::endl;

    auto point = (int*)std::malloc(1 * sizeof(int));
    //打印未知字符
    std::cout << "std::malloc: " << point[0] << std::endl;

    MyString* point1 = (MyString*)std::calloc(4, sizeof(MyString)); // 分配并清零 4 个 int 的数组
    MyString* point2 = (MyString*)std::calloc(1, sizeof(MyString[4])); // 同上,直接指名数组类型
    MyString* point3 = (MyString*)std::calloc(4, sizeof * point3);  // 同上,不重复类型名

    if (point2)
    {
        for (int n = 0; n < 4; ++n) // 打印数组
        {
            std::cout << "point2[" << n << "] == " << point2[n] << std::endl;
        }
    }

    std::free(point1);
    std::free(point2);
    std::free(point3);

    return 0;
}

输出

MyString  0xf57ab8
MyString  0xf57ad0
MyString  0xf57ae8
MyString  0xf57b00
MyString  0xf57b18
point[0] == aaaaa
point[1] == bbbbb
point[2] == ccccc
point[3] == ddddd
point[4] == eeeee
~MyString  0xf57b18
~MyString  0xf57b00
~MyString  0xf57ae8
~MyString  0xf57ad0
~MyString  0xf57ab8



std::malloc: 16060224
point2[0] ==
point2[1] ==
point2[2] ==
point2[3] ==

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值