C++标准模板(STL)- C 内存管理库 - 分配并清零内存 (std::calloc)

C 内存管理库

分配并清零内存

复制代码
std::calloc

|-----------------------------------------------------|---|---|
| void* calloc( std::size_t num, std::size_t size ); | | |

分配 num 个大小为 size 的对象的数组,并初始化所有位为零。

若分配成功,则返回指向为任何对象类型适当对齐的,被分配内存块最低(首)字节的指针。

size 为零,则行为是实现定义的(可以返回空指针,亦可返回某个不可用于访问存储的非空指针)

|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------|
| 要求下列函数是线程安全的: * operator new 及 operator delete 的库版本 * 全局 operator new 与 operator delete 的用户替换版本 * std::calloc 、 std::malloc 、 std::realloc 、 std::aligned_alloc (C++17 起) 、 std::free 对这些分配或解分配特定存储单元的函数调用以单独全序出现,并且在此顺序中,每个解分配调用先发生于下个分配(若存在)。 | (C++11 起) |

参数

|------|---|---------|
| num | - | 对象数量 |
| size | - | 每个对象的大小 |

返回值

成功时,返回指向新分配内存起始的指针。返回的指针必须以 std::free() 或 std::realloc() 解分配。

失败时,返回空指针。

注意

因为对齐要求,分配的字节数不需要等于 num*size

将所有位初始化为零不保证浮点数或指针各被初始化到 0.0 与空指针值(尽管大多数平台上这为 true )

最初( C89 中),添加对零大小的支持是为了接纳下面这种代码

调用示例

复制代码
#include <iostream>
#include <cstdlib>
#include <string>

class MyString : public std::string
{
public:
    MyString() : std::string("GGX")
    {
        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()
{
    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;
}

输出

复制代码
std::malloc: 16387904
point2[0] ==
point2[1] ==
point2[2] ==
point2[3] ==
相关推荐
逆小舟5 分钟前
【Linux】人事档案——用户及组管理
linux·c++
l1t8 分钟前
利用DeepSeek实现服务器客户端模式的DuckDB原型
服务器·c语言·数据库·人工智能·postgresql·协议·duckdb
再见晴天*_*1 小时前
SpringBoot 中单独一个类中运行main方法报错:找不到或无法加载主类
java·开发语言·intellij idea
l1t2 小时前
利用美团龙猫用libxml2编写XML转CSV文件C程序
xml·c语言·libxml2·解析器
lqjun08273 小时前
Qt程序单独运行报错问题
开发语言·qt
hdsoft_huge4 小时前
Java & Spring Boot常见异常全解析:原因、危害、处理与防范
java·开发语言·spring boot
风中的微尘5 小时前
39.网络流入门
开发语言·网络·c++·算法
未来之窗软件服务5 小时前
幽冥大陆(二)RDIFSDK 接口文档:布草洗涤厂高效运营的技术桥梁C#—东方仙盟
开发语言·c#·rdif·仙盟创梦ide·东方仙盟
混分巨兽龙某某5 小时前
基于Qt Creator的Serial Port串口调试助手项目(代码开源)
c++·qt creator·串口助手·serial port
小冯记录编程6 小时前
C++指针陷阱:高效背后的致命危险
开发语言·c++·visual studio