C 库函数 - free()

C 语言教程 · 2019-02-24 07:28:06

描述

C 库函数 void free(void *ptr) 释放之前调用 calloc、malloc 或 realloc 所分配的内存空间。

声明

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

void free(void *ptr)

参数

  • ptr -- 指针指向一个要释放内存的内存块,该内存块之前是通过调用 malloc、calloc 或 realloc 进行分配内存的。如果传递的参数是一个空指针,则不会执行任何动作。

返回值

该函数不返回任何值。

实例

下面的实例演示了 free() 函数的用法。

实例

#include <stdio.h> #include <stdlib.h> #include <string.h> int main() { char *str; /* 最初的内存分配 */ str = (char *) malloc(15); strcpy(str, "codercto"); printf("String = %s, Address = %s\n", str, str); /* 重新分配内存 */ str = (char *) realloc(str, 25); strcat(str, ".com"); printf("String = %s, Address = %s\n", str, str); /* 释放已分配的内存 */ free(str); return(0); }

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

String = codercto,  Address = 3351249760
String = codercto.com,  Address = 3351249760

点击查看所有 C 语言教程 文章: https://codercto.com/courses/l/17.html

查看所有标签

HTML & XHTML

HTML & XHTML

Chuck Musciano、Bill Kennedy / O'Reilly Media / 2006-10-27 / GBP 39.99

"...lucid, in-depth descriptions of the behavior of every HTML tag on every major browser and platform, plus enough dry humor to make the book a pleasure to read." --Edward Mendelson, PC Magazine "Whe......一起来看看 《HTML & XHTML》 这本书的介绍吧!

URL 编码/解码
URL 编码/解码

URL 编码/解码

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具