Skip to Content
C语言核心技术(原书第2版)
book

C语言核心技术(原书第2版)

by Peter Prinz, Tony Crawford
August 2017
Intermediate to advanced
798 pages
17h 44m
Chinese
China Machine Press
Content preview from C语言核心技术(原书第2版)
190
第12章
动态内存管理
在编写程序时,通常并不知道需要处理的数据量,或者难以评估所需处理数据量的变动
程度。在这种情况下,要达到有效的资源利用,必须在运行时动态地分配所需内存,并
在使用完毕后尽早释放不需要的内存。这就是动态内存管理原理,它同时还具有一个优
点:当程序在具有更多内存的系统上需要处理更多数据时,不需要重写程序。
本章讨论
C
语言的动态内存管理机制,并以通用二叉树的实现作为示例,介绍动态内存
管理中最重要的相关函数。
标准库提供以下四个函数用于动态内存管理:
malloc()
、
calloc()
分配新的内存区域。
realloc()
调整已分配的内存区域。
free()
释放已分配的内存区域。
上述所有函数都声明在头文件
stdlib.h
中。对象在内存中所占空间的大小是以字节数量为
单位计算的。许多头文件(包括
stdlib.h
)专门定义了类型
size_t
用来保存这种内存空间
的相关信息。例如,
sizeof
运算符以类型
size_t
返回字节数量。
12.1 动态内存分配
两个内存分配函数
malloc()
和
calloc()
的
参数不一样:
动态内存管理
|
191
动态内存管理
void *malloc( size_t
size
);
函数
malloc()
分配
连续的内存区域,其大小不小于
size
。当程序通过
malloc ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

算法精讲视频课程:24篇系列讲座

算法精讲视频课程:24篇系列讲座

罗伯特·塞奇威克, 凯文·韦恩(Kevin Wayne)

Publisher Resources

ISBN: 9787111562207