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系統程式設計 第二版
320
|
第九章
return p;
}
調整記憶體分配的尺寸
C
語言提供了一個介面,可用於調整既有記憶體分配的尺寸(變大或變小):
#include <stdlib.h>
void * realloc (void *ptr, size_t size);
執行成功時,
realloc()
會把
ptr
所指向之記憶體範圍的尺寸調整為新尺寸
size
個位元
組。它會傳回一個指標,指向剛調整過尺寸的記憶體範圍,這個指標可能跟
ptr
一樣,
也可能不一樣。擴大一個記憶體範圍時,如果
realloc()
無法在原處透過擴張團塊的方
式來擴大既有的記憶體團塊,則此函式會分配一個新的記憶體範圍(長度為
size
個位元
組),然後把舊範圍的內容複製到新範圍,並且釋出舊的範圍。不管是哪種情況,記憶
體範圍的內容被保存下來的部分,取決於舊尺寸與新尺寸中最小者。因為可能會進行複
製操作,所以使用
realloc()
擴大一個記憶體範圍,可能是一個代價相當高的行動。
如果
size
的值為
0
,則結果如同針對
ptr
調用
free()
。
如果
ptr
的值為
NULL
,則此操作的結果如同
malloc()
。如果
ptr
的值非
NULL
,它必須是
之前所進行之
malloc()
、
calloc()
或
realloc()
呼叫的傳回值。
執行失敗時,
realloc()
會傳回
NULL
以及把
errno
設定為
ENOMEM
。而且
ptr
所指向之記憶
體的狀態不會改變。
讓我們來看一個縮小記憶體範圍的例子。首先,我們會使用
calloc()
分配足夠的記憶體
範圍,以便存放具有兩個元素之
map
結構(所構成的)陣列: ...
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