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

40 lines
1002 B
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>动态内存 - malloc</h1>
<p>原型extern void *malloc(unsigned int num_bytes);</p>
<p>用法:#include &lt;alloc.h></p>
<p>功能分配长度为num_bytes字节的内存块</p>
<p>说明如果分配成功则返回指向被分配内存的指针否则返回空指针NULL。</p>
当内存不再使用时应使用free()函数将内存块释放。
举例:<pre><code class="language-c">
// malloc.c
#include &lt;syslib.h>
#include &lt;alloc.h>
main()
{
char *p;
clrscr(); // clear screen
p=(char *)malloc(100);
if(p)
printf("Memory Allocated at: %x",p);
else
printf("Not Enough Memory!\n");
free(p);
getchar();
return 0;
}
</code></pre>相关函数:<a href="calloc.html">calloc</a>,<a href="free.html">free</a>,<a href="realloc.html">realloc</a>