free() – C语言库函数
C库函数 void free(void *ptr) 由calloc,malloc或realloc调用先前分配的回收内存。
声明
以下是free()函数的声明。
void free(void*ptr)
参数
-
ptr -- 这是用malloc,calloc的或realloc被释放以前分配的内存块的指针。如果一个空指针作为参数传递,不会发生任何动作
返回值
这个函数不返回任何值。
例子
下面的例子演示了如何使用free() 函数。
#include<stdio.h>#include<stdlib.h>int main(){char*str;/* Initial memory allocation */ str =(char*) malloc(15); strcpy(str,"yiibai"); printf("String = %s, Address = %u ", str, str);/* Reallocating memory */ str =(char*) realloc(str,25); strcat(str,".com"); printf("String = %s, Address = %u ", str, str);/* Deallocate allocated memory */ free(str);return(0);}
让我们编译和运行上面的程序,这将产生以下结果:
String = yiibai, Address = 355090448 String = xyhtml5.com, Address = 355090448