free(C語言提供的庫函式)

原型: void free(void *ptr)
功 能: 釋放ptr指向的存儲空間。被釋放的空間通常被送入可用存儲區池,以後可在調用malloc、realloc以及calloc函式來再分配。
程式例:
#include <string.h>
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
int main(void)
{
char *str;
/* allocate memory for string */
str = (char *)malloc(10);
if(str == NULL){
perror("malloc");
exit(1);
}
/* copy "Hello" to string */
strcpy(str, "Hello");
/* display string */
printf("String is %s\n", str);
/* free memory */
free(str);
return 0;
}

相關詞條

熱門詞條

聯絡我們