mirror of
https://github.com/fofolee/uTools-Manuals.git
synced 2025-06-08 06:55:36 +08:00
40 lines
963 B
HTML
40 lines
963 B
HTML
<h1>动态内存 - malloc</h1>
|
||
|
||
|
||
<p>原型:extern void *malloc(unsigned int num_bytes);</p>
|
||
|
||
<p>用法:#include <alloc.h></p>
|
||
|
||
<p>功能:分配长度为num_bytes字节的内存块</p>
|
||
|
||
<p>说明:如果分配成功则返回指向被分配内存的指针,否则返回空指针NULL。</p>
|
||
当内存不再使用时,应使用free()函数将内存块释放。
|
||
|
||
举例:<pre><code class="language-c">
|
||
|
||
// malloc.c
|
||
|
||
#include <syslib.h>
|
||
#include <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>
|
||
|