标签:文件 new 内存分配 动态内存 signed printf 函数 int include
malloc calloc realloc 都是<stdilb.h>头文件的函数
函数原型:
void malloc(unsigned int num_bytes)
void calloc(size_t n, size_t size)
void realloc(void *ptr, size_t new_Size)
先说malloc和calloc区别
malloc分配的空间里都是垃圾值,而calloc里全是0
但是malloc的效率比calloc的高
# include <stdio.h>
# include <malloc.h>
int main()
{
int* p;
int* pp;
p = (int*)malloc(10 * sizeof(int));
pp = (int*)calloc(10 ,sizeof(int));
printf("malloc的存储空间:\n");
for (int i = 0; i < 10; i++)
{
printf("%d ", *(p+i));
}
printf("\n");
printf("calloc的存储空间:\n");
for (int i = 0; i < 10; i++)
{
printf("%d ", *(pp+i));
}
printf("\n");
return 0;
}
标签:文件 new 内存分配 动态内存 signed printf 函数 int include
原文地址:https://www.cnblogs.com/panghushalu/p/11956916.html