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://www.codercto.com/courses/l/17.html

查看所有标签

Game Engine Architecture, Second Edition

Game Engine Architecture, Second Edition

Jason Gregory / A K Peters/CRC Press / 2014-8-15 / USD 69.95

A 2010 CHOICE outstanding academic title, this updated book covers the theory and practice of game engine software development. It explains practical concepts and techniques used by real game studios,......一起来看看 《Game Engine Architecture, Second Edition》 这本书的介绍吧!

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具

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

Markdown 在线编辑器

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具