mirror of
https://github.com/fofolee/uTools-Manuals.git
synced 2025-06-08 23:14:06 +08:00
50 lines
1.2 KiB
HTML
50 lines
1.2 KiB
HTML
<h1>动态内存 - free</h1>
|
||
|
||
|
||
<p>原型:extern void free(void *p);</p>
|
||
|
||
<p>用法:#include <alloc.h></p>
|
||
|
||
<p>功能:释放指针p所指向的的内存空间。</p>
|
||
|
||
<p>说明:p所指向的内存空间必须是用calloc,malloc,realloc所分配的内存。
|
||
如果p为NULL或指向不存在的内存块则不做任何操作。</p>
|
||
|
||
举例:<pre>
|
||
|
||
// free.c
|
||
|
||
#include <syslib.h>
|
||
#include <alloc.h>
|
||
|
||
main()
|
||
{
|
||
char *p;
|
||
|
||
clrscr(); // clear screen
|
||
textmode(0x00);
|
||
|
||
p=(char *)malloc(100);
|
||
if(p)
|
||
printf("Memory Allocated at: %x",p);
|
||
else
|
||
printf("Not Enough Memory!\n");
|
||
|
||
getchar();
|
||
free(p); // release memory to reuse it
|
||
|
||
p=(char *)calloc(100,1);
|
||
if(p)
|
||
printf("Memory Reallocated at: %x",p);
|
||
else
|
||
printf("Not Enough Memory!\n");
|
||
|
||
free(p); // release memory at program end
|
||
|
||
getchar();
|
||
return 0;
|
||
}
|
||
|
||
</pre>相关函数:<a href="calloc.html">calloc</a>,<a href="malloc.html">malloc</a>,<a href="realloc.html">realloc</a>
|
||
|