外贸公司如何做公司网站,设计一个完整的静态网站,少儿编程培训机构哪里好,照片网站模版C 内存管理库
分配并清零内存
std::calloc void* calloc( std::size_t num, std::size_t size );
分配 num 个大小为 size 的对象的数组#xff0c;并初始化所有位为零。
若分配成功#xff0c;则返回指向为任何对象类型适当对齐的#xff0c;被分配内存块最低#xf…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 (C17 起) 、 std::free对这些分配或解分配特定存储单元的函数调用以单独全序出现并且在此顺序中每个解分配调用先发生于下个分配若存在。 (C11 起)
参数
num-对象数量size-每个对象的大小
返回值
成功时返回指向新分配内存起始的指针。返回的指针必须以 std::free() 或 std::realloc() 解分配。
失败时返回空指针。
注意
因为对齐要求分配的字节数不需要等于 num*size 。
将所有位初始化为零不保证浮点数或指针各被初始化到 0.0 与空指针值尽管大多数平台上这为 true
最初 C89 中添加对零大小的支持是为了接纳下面这种代码 调用示例
#include iostream
#include cstdlib
#include stringclass 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_castvoid *(this) std::endl;}~MyString(){this-~basic_string();std::cout __FUNCTION__ static_castvoid *(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]