free函数

free函数
原型: 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;
}
posted @ 2014-08-29 16:19  SilenceBook  阅读(499)  评论(0编辑  收藏  举报