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] ==
相关推荐
猷咪9 分钟前
C++基础
开发语言·c++
IT·小灰灰11 分钟前
30行PHP,利用硅基流动API,网页客服瞬间上线
开发语言·人工智能·aigc·php
快点好好学习吧12 分钟前
phpize 依赖 php-config 获取 PHP 信息的庖丁解牛
android·开发语言·php
秦老师Q13 分钟前
php入门教程(超详细,一篇就够了!!!)
开发语言·mysql·php·db
烟锁池塘柳013 分钟前
解决Google Scholar “We‘re sorry... but your computer or network may be sending automated queries.”的问题
开发语言
是誰萆微了承諾13 分钟前
php 对接deepseek
android·开发语言·php
CSDN_RTKLIB16 分钟前
WideCharToMultiByte与T2A
c++
2601_9498683617 分钟前
Flutter for OpenHarmony 电子合同签署App实战 - 已签合同实现
java·开发语言·flutter
飞机和胖和黄29 分钟前
考研之王道C语言第三周
c语言·数据结构·考研
星火开发设计31 分钟前
类型别名 typedef:让复杂类型更简洁
开发语言·c++·学习·算法·函数·知识