mirror of
https://github.com/fofolee/uTools-Manuals.git
synced 2025-06-08 06:55:36 +08:00
48 lines
1.2 KiB
HTML
48 lines
1.2 KiB
HTML
<h1>动态内存 - realloc</h1>
|
||
|
||
|
||
<p>原型:extern void *realloc(void *mem_address, unsigned int newsize);</p>
|
||
|
||
<p>用法:#include <alloc.h></p>
|
||
|
||
<p>功能:改变mem_address所指内存区域的大小为newsize长度。</p>
|
||
|
||
<p>说明:如果重新分配成功则返回指向被分配内存的指针,否则返回空指针NULL。</p>
|
||
当内存不再使用时,应使用free()函数将内存块释放。
|
||
|
||
举例:<pre><code class="language-c">
|
||
|
||
// realloc.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");
|
||
|
||
getchar();
|
||
|
||
p=(char *)realloc(p,256);
|
||
if(p)
|
||
printf("Memory Reallocated 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="malloc.html">malloc</a>
|
||
|