目录

void free(void * ptr(void free(void *ptr)

描述 (Description)

C库函数void free(void *ptr)释放先前通过调用calloc,malloc或realloc分配的内存。

声明 (Declaration)

以下是free()函数的声明。

void free(void *ptr)

参数 (Parameters)

  • ptr - 这是指向先前分配有要释放的malloc,calloc或realloc的内存块的指针。 如果将空指针作为参数传递,则不会执行任何操作。

返回值 (Return Value)

此函数不返回任何值。

例子 (Example)

以下示例显示了free()函数的用法。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main () {
   char *str;
   /* Initial memory allocation */
   str = (char *) malloc(15);
   strcpy(str, "iowiki");
   printf("String = %s,  Address = %u\n", str, str);
   /* Reallocating memory */
   str = (char *) realloc(str, 25);
   strcat(str, ".com");
   printf("String = %s,  Address = %u\n", str, str);
   /* Deallocate allocated memory */
   free(str);
   return(0);
}

让我们编译并运行上面的程序,它将产生以下结果 -

String = iowiki, Address = 355090448
String = iowiki.com, Address = 355090448
↑回到顶部↑
WIKI教程 @2018