#include <stdlib.h>
void free( void *ptr );
函数释放指针 ptr 指向的空间,以供以后使用。指针 ptr 必须由先前对 malloc(), calloc(), realloc() 的调用返回。
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char *str = NULL;
/* allocate memory for string */
str = calloc(10, sizeof(char));
/* copy "Hello" into string */
strcpy(str, "Hello");
/* display string */
printf("String is %s\n", str);
/* free memory */
free(str);
return 0;
}输出结果:
String is Hello