uTools-Manuals/docs/c/calloc.html
2019-04-21 11:50:48 +08:00

40 lines
1.0 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<h1>动态内存 - calloc</h1>
<p>原型extern void *calloc(int num_elems, int elem_size);</p>
<p>用法:#include &lt;alloc.h></p>
<p>功能为具有num_elems个长度为elem_size元素的数组分配内存</p>
<p>说明如果分配成功则返回指向被分配内存的指针否则返回空指针NULL。<br>
当内存不再使用时应使用free()函数将内存块释放。</p>
举例:<pre><code class="language-c">
// calloc.c
#include &lt;syslib.h>
#include &lt;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>