Skip to Content
LINUX系統程式設計 第二版
book

LINUX系統程式設計 第二版

by Robert Love
December 2013
Intermediate to advanced
496 pages
8h 57m
Chinese
GoTop Information, Inc.
Content preview from LINUX系統程式設計 第二版
316
|
第九章
Linux 會透過兩種方法來優化這些變數。首先,因為 bss 區段只用於存放未初始化的資
料,所以鏈結器(
ld
)實際上不會在目的碼檔(object file)中儲存這些特殊值。如此可
以縮減執行檔的尺寸。其次,當此區段被載入至記憶體時,核心會以寫入時才複製的做
法將此區段映射至一個填滿零的頁面,有效地為這些變數設定預定值。
多數位址空間會包含一些映射檔,例如程式的可執行檔本身、C 與其他的
共享程式庫以及資料檔。檔案
/proc/self/maps
pmap
程式的輸出,可以看
到一個行程會包含哪些映射檔。
本章接下來將說明 Linux 所提供「用於取得與傳回記憶體、建立與銷毀新的映射以及其
間一切」的介面。
分配動態記憶體
記憶體還會以自動及靜態變數的形式出現,但是任何記憶體管理系統的基礎是
動態記
憶體
dynamic memory
)的分配、使用以及最後的傳回。動態記憶體的分配在執行時期
(而非編譯時期)進行,分配之前它的大小可能是未知的。身為一個開發者,會在以下
情況下用到動態記憶體:需要相當數量的記憶體,或是這些記憶體需要使用多久不一
定,在程式執行之前無法知道。例如,你可能會想要把一個檔案的內容,或是讀取自鍵
盤的輸入資料,存放在記憶體中。因為不知道檔案的大小,而且用戶可能會按下任何數
目的鍵,所以無法確定緩衝區的大小,於是你可能會因為所讀取到的資料越來越多,動
態調大緩衝區的尺寸。
C 語言的變數本身並不會用到動態記憶體。例如,C 語言並未提供任何機制,可用於取
得一個存在於動態記憶體的
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

AGILE學習手冊 | SCRUM、XP、精實和看板方法

AGILE學習手冊 | SCRUM、XP、精實和看板方法

Andrew Stellman, Jennifer Greene
管理Kubernetes

管理Kubernetes

Brendan Burns, Craig Tracey
算法技术手册(原书第2 版)

算法技术手册(原书第2 版)

George T.Heineman, Gary Pollice, Stanley Selkow

Publisher Resources

ISBN: 9789862769812