Skip to Content
For Enterprise
For Government
For Higher Ed
For Individuals
For Marketing
For Enterprise
For Government
For Higher Ed
For Individuals
For Marketing
Explore Skills
Cloud Computing
Microsoft Azure
Amazon Web Services (AWS)
Google Cloud
Cloud Migration
Cloud Deployment
Cloud Platforms
Data Engineering
Data Warehouse
SQL
Apache Spark
Microsoft SQL Server
MySQL
Kafka
Data Lake
Streaming & Messaging
NoSQL Databases
Relational Databases
Data Science
Pandas
R
MATLAB
SAS
D3
Power BI
Tableau
Statistics
Exploratory Data Analysis
Data Visualization
AI & ML
Generative AI
Machine Learning
Artificial Intelligence (AI)
Deep Learning
Reinforcement Learning
Natural Language Processing
TensorFlow
Scikit-Learn
Hyperparameter Tuning
MLOps
Programming Languages
Java
JavaScript
Spring
Python
Go
C#
C++
C
Swift
Rust
Functional Programming
Software Architecture
Object-Oriented
Distributed Systems
Domain-Driven Design
Architectural Patterns
IT/Ops
Kubernetes
Docker
GitHub
Terraform
Continuous Delivery
Continuous Integration
Database Administration
Computer Networking
Operating Systems
IT Certifications
Security
Network Security
Application Security
Incident Response
Zero Trust Model
Disaster Recovery
Penetration Testing / Ethical Hacking
Governance
Malware
Security Architecture
Security Engineering
Security Certifications
Design
Web Design
Graphic Design
Interaction Design
Film & Video
User Experience (UX)
Design Process
Design Tools
Business
Agile
Project Management
Product Management
Marketing
Human Resources
Finance
Team Management
Business Strategy
Digital Transformation
Organizational Leadership
Soft Skills
Professional Communication
Emotional Intelligence
Presentation Skills
Innovation
Critical Thinking
Public Speaking
Collaboration
Personal Productivity
Confidence / Motivation
Features
All features
Verifiable skills
AI Academy
Courses
Certifications
Interactive learning
Live events
Superstreams
Answers
Insights reporting
Radar Blog
Buy Courses
Plans
Sign In
Try Now
O'Reilly Platform
book
LINUX系統程式設計 第二版
by
Robert Love
December 2013
Intermediate to advanced
496 pages
8h 57m
Chinese
GoTop Information, Inc.
Content preview from
LINUX系統程式設計 第二版
記憶體管理
|
321
現在,假設我們已經找到其中一份寶藏,不再需要第二張地圖,所以我們決定調整記憶
體的尺寸,以便將範圍的一半還回系統。一般而言這麼做並不值得,但是,如果
map
結
構非常大,而且我們打算長時間保存地圖,就值得這麼做:
struct map *r;
/*
現在我們只需要足以保存一張地圖的記憶體範圍
*/
r = realloc (p, sizeof (struct map));
if (!r) {
/*
注意,
'p'
仍然有效!
*/
perror ("realloc");
return
−
1;
}
/*
使用
'r'... */
free (r);
此例中,叫用
realloc()
之後,
p[0]
會被保留下來。之前的資料仍然存在於該處。如果
失敗,則
p
維持不變,因此仍然有效。我們可以繼續使用它,而且最後需要釋出它。反
過來說,如果成功,我們可以忽略
p
,並轉而使用
r
(可能跟
p
一樣,因為新的記憶體範
圍幾乎一定是就地調整尺寸)。現在,當我們完成工作之後,我們有責任釋出
r
。
釋出動態記憶體
不同於自動分配(當
stack unwind
4
時會自動解除分配),動態分配記憶體是行程之位址
空間中永久保存一部分,除非它們被手動釋出。因此,程式設計者必須承擔把動態分配
的記憶體釋回系統的責任。(當然,如果整個行程結束運作,則靜態和動態分配的記憶
體範圍都會不見。)
使用
malloc()
、
calloc()
或
realloc()
所分配到的記憶體,不再使用時必須經由
free()
釋回給系統:
#include <stdlib.h> ...
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.
Start your free trial
You might also like
優雅的SciPy|Python科學研究的美學
Juan Nunez-Iglesias, Stéfan van der Walt, Harriet Dashnow
成為卓越程式設計師的38項必修法則
Pete Goodliffe
深入理解運算原理|從簡單的機器到無所不能的程式
Tom Stuart
C++语言导学(原书第2版)
本贾尼 斯特劳斯特鲁普
Publisher Resources
ISBN: 9789862769812