free()的用法----释放内存

功 能: 与malloc()函数配对使用,释放malloc函数申请的动态内存。(另:若是p 是NULL 指针,那么free 对p 不管操做多少次都不会出问题。若是p 不是NULL 指针,那么free 对p连续操做两次就会致使程序运行错误。)函数

用 法: void free(void *ptr);指针

程序例:htm

#include <string.h>内存

#include <stdio.h>get

#include <alloc.h> //or #include <malloc.h>string

int main(void)io

{程序

char *str;di

/* allocate memory for string */view

str = (char *)malloc(10);

/* copy "Hello" to string */

strcpy(str, "Hello");

/* display string */

printf("String is %s\n", str);

/* free memory */

free(str);

return 0;

}

相关文章
相关标签/搜索