C stdlib calloc() 函数
示例
分配内存并写入一些值
// Allocate memory for a number of items
int numItems = 15;
int *myArray = calloc(numItems, sizeof(int));
// Write into the memory
for(int i = 0; i < numItems; i++) {
myArray[i] = i + 1;
}
// Display the contents of the memory
for(int i = 0; i < numItems; i++) {
printf("%d ", myArray[i]);
}
// Free the memory
free(myArray);
myArray = NULL;
自己动手试一试 »
定义和用法
calloc()
函数分配内存,用零填充它,并返回指向它的指针。
calloc()
函数定义在 <stdlib.h>
头文件中。
要了解更多关于内存分配的信息,请参阅我们的 C 内存管理教程。
语法
calloc(size_t amount, size_t size);
size_t
数据类型是一个非负整数。
参数值
参数 | 描述 |
---|---|
amount | 指定要为其分配内存的项的数量。 |
大小 | 指定每个项的大小(以字节为单位)。 |
技术详情
返回 | 指向新分配内存块的 void * 指针。 |
---|