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

查看所有标签

Charlotte's Web

Charlotte's Web

E. B. White / Puffin Classics / 2010-6-3 / GBP 6.99

This is the story of a little girl named Fern who loved a little pig named Wilbur and of Wilbur's dear friend, Charlotte A. Cavatica, a beautiful large grey spider. With the unlikely help of Templeton......一起来看看 《Charlotte's Web》 这本书的介绍吧!

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具

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

HSV CMYK互换工具