calloc
连续内存块分配并且初始化内存为0。
void *calloc(size_t number_of_objects, size_t size_of_object);
参数
参数名 |
描述 |
取值范围 |
输入/输出 |
|---|---|---|---|
number_of_objects |
申请内存块的数量。 |
非负数 |
输入 |
size_of_object |
申请内存块的字节数。 |
非负数 |
输入 |
返回值
- 成功:返回指向分配内存的指针。
- 失败:返回空指针。
示例
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include "kqmalloc.h"
#define KQMALLOC_TEST_LEN 10
void CallocExample()
{
int8_t *res = (int8_t *)calloc(KQMALLOC_TEST_LEN, KQMALLOC_TEST_LEN);
if (res == NULL) {
printf("res is null\n");
} else {
printf("calloc pointer address: %lx\n", res);
free(res);
}
}
int main(void) {
CallocExample();
return 0;
}
运行结果:
calloc pointer address: ffffafe4c690
父主题: 函数说明