mirror of
https://github.com/fofolee/uTools-Manuals.git
synced 2025-06-08 15:04:05 +08:00
40 lines
1.0 KiB
HTML
40 lines
1.0 KiB
HTML
<h1>动态内存 - calloc</h1>
|
||
|
||
|
||
<p>原型:extern void *calloc(int num_elems, int elem_size);</p>
|
||
|
||
<p>用法:#include <alloc.h></p>
|
||
|
||
<p>功能:为具有num_elems个长度为elem_size元素的数组分配内存</p>
|
||
|
||
<p>说明:如果分配成功则返回指向被分配内存的指针,否则返回空指针NULL。<br>
|
||
当内存不再使用时,应使用free()函数将内存块释放。</p>
|
||
|
||
举例:<pre><code class="language-c">
|
||
|
||
// 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;
|
||
}
|
||
|
||
</code></pre>相关函数:<a href="free.html">free</a>,<a href="malloc.html">malloc</a>,<a href="realloc.html">realloc</a>
|
||
|