阅读(4006) (0)

calloc

2015-11-11 17:00:41 更新

 原型:extern void *calloc(int num_elems, int elem_size);
 
 用法:#include <alloc.h>
 
 功能:为具有num_elems个长度为elem_size元素的数组分配内存
 
 说明:如果分配成功则返回指向被分配内存的指针,否则返回空指针NULL。
       当内存不再使用时,应使用free()函数将内存块释放。
 
 举例:

     // calloc.c
     
     #include <syslib.h>
     #include <alloc.h>
     main()
     {
       char *p;
       
       clrscr();        // clear screen
       p=(char *)calloc(100,sizeof(char));
       if(p)
         printf("Memory Allocated at: %x",p);
       else
         printf("Not Enough Memory!\n");
         
       free(p);
       getchar();
       return 0;
     }