C 標(biāo)準(zhǔn)庫(kù) - <stdlib.h>
C 庫(kù)函數(shù) void free(void *ptr) 釋放之前調(diào)用 calloc、malloc 或 realloc 所分配的內(nèi)存空間。
下面是 free() 函數(shù)的聲明。
void free(void *ptr)
該函數(shù)不返回任何值。
下面的示例演示了 free() 函數(shù)的用法。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char *str;
/* 最初的內(nèi)存分配 */
str = (char *) malloc(15);
strcpy(str, "nhooo");
printf("String = %s, Address = %p\n", str, str);
/* 重新分配內(nèi)存 */
str = (char *) realloc(str, 25);
strcat(str, ".com");
printf("String = %s, Address = %p\n", str, str);
/* 釋放已分配的內(nèi)存 */
free(str);
return(0);
}讓我們編譯并運(yùn)行上面的程序,這將產(chǎn)生以下結(jié)果:
String = nhooo, Address = 0x7fe4e4c02b10 String = (cainiaoplus.com), Address = 0x7fe4e4c02b10