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

48 lines
1.2 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>动态内存 - realloc</h1>
<p>原型extern void *realloc(void *mem_address, unsigned int newsize);</p>
<p>用法:#include &lt;alloc.h></p>
<p>功能改变mem_address所指内存区域的大小为newsize长度。</p>
<p>说明如果重新分配成功则返回指向被分配内存的指针否则返回空指针NULL。</p>
当内存不再使用时应使用free()函数将内存块释放。
举例:<pre><code class="language-c">
// realloc.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");
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>