void *calloc(unsigned n,unsigned size); 頭文件爲:stdlib.h函數
功 能: 在內存的動態存儲區中分配n個長度爲size的連續空間,函數返回一個指向分配起始地址的指針;若是分配不成功,返回NULL。指針
跟malloc的區別: calloc在動態分配完內存後,自動初始化該內存空間爲零,而malloc不初始化,裏邊數據是隨機的垃圾數據。而且malloc申請的內存能夠是不連續的,而calloc申請的內存空間必須是連續的。code
示例代碼:內存
<!-- lang: cpp --> #include<stdio.h> #include<stdlib.h> #include<string.h> int main() { char *pcName; pcName = (char *)calloc(10UL, sizeof(char)); printf("%s\n\n",pcName); printf("The length is %d\n",strlen(pcName)); return 0; }
malloc代碼:string
<!-- lang: cpp --> #include<stdio.h> #include<stdlib.h> #include<string.h> int main() { char *pcName; pcName = (char *)malloc(10*sizeof(char)); printf("%s\n\n",pcName); printf("The length is %d\n",strlen(pcName)); return 0; }