当前位置首页 > 百科> 正文

free()

2019-07-30 00:04:06 百科
free()

free()

C语言函式

头档案:malloc.h或stdlib.h

作用:释放malloc(或calloc、realloc)函式给指针变数分配的记忆体空间。

注意:使用后该指针变数一定要重新指向NULL,防止野指针出现,有效规避错误操作。

基本介绍

  • 中文名:free()
  • 属性:C语言函式
  • 头档案:malloc.h或stdlib.h
  • 作用:释放记忆体空间

简介

函式名: free
功 能: 与malloc()函式配对使用,释放malloc函式申请的动态记忆体。(另:对于free(p)这句语句,如果p 是NULL 指针,那幺free 对p 无论操作多少次都不会出问题。如果p 不是NULL 指针,那幺free 对p连续操作两次就会导致程式运行错误。)
用 法: void free(void *ptr);

程式例:

C/C++代码如下:
#include <string.h>#include <stdio.h>#include <alloc.h> //or #include <malloc.h>int main(void){char *str;/* allocate memory for string */str = (char *)malloc(10);/* copy "Hello" to string */strcpy(str, "Hello");/* display string */printf("String is %s\n", str);/* free memory */free(str);str=NULL;return 0;}
声明:此文信息来源于网络,登载此文只为提供信息参考,并不用于任何商业目的。如有侵权,请及时联系我们:baisebaisebaise@yeah.net