1. 程式人生 > >【轉載】C++自由存儲區是否等價於堆

【轉載】C++自由存儲區是否等價於堆

destroy 對象池 when for default 維護 iat glob delete

“free store” VS “heap”

當我問你C++的內存布局時,你大概會回答:

“在C++中,內存區分為5個區,分別是堆、棧、自由存儲區、全局/靜態存儲區、常量存儲區”。

如果我接著問你自由存儲區與堆有什麽區別,你或許這樣回答:

“malloc在堆上分配的內存塊,使用free釋放內存,而new所申請的內存則是在自由存儲區上,使用delete來釋放。”

這樣聽起來似乎也沒錯,但如果我接著問:

自由存儲區與堆是兩塊不同的內存區域嗎?它們有可能相同嗎?

你可能就懵了。

事實上,我在網上看的很多博客,劃分自由存儲區與堆的分界線就是new/delete與malloc/free。然而,盡管C++標準沒有要求,但很多編譯器的new/delete都是以malloc/free為基礎來實現的。那麽請問:借以malloc實現的new,所申請的內存是在堆上還是在自由存儲區上?

從技術上來說,堆(heap)是C語言和操作系統的術語。堆是操作系統所維護的一塊特殊內存,它提供了動態分配的功能,當運行程序調用malloc()時就會從中分配,稍後調用free可把內存交還。而自由存儲是C++中通過new和delete動態分配和釋放對象的抽象概念,通過new來申請的內存區域可稱為自由存儲區。基本上,所有的C++編譯器默認使用堆來實現自由存儲,也即是缺省的全局運算符new和delete也許會按照malloc和free的方式來被實現,這時藉由new運算符分配的對象,說它在堆上也對,說它在自由存儲區上也正確。但程序員也可以通過重載操作符,改用其他內存來實現自由存儲,例如全局變量做的對象池,這時自由存儲區就區別於堆了。我們所需要記住的就是:

堆是操作系統維護的一塊內存,而自由存儲是C++中通過new與delete動態分配和釋放對象的抽象概念。堆與自由存儲區並不等價。

問題的來源

再回過頭來來看看這個問題的起源在哪裏。最先我們使用C語言的時候,並沒有這樣的爭議,很明確地知道malloc/free是在堆上進行內存操作。直到我們在Bjarne Stroustrup的書籍中數次看到free store (自由存儲區),說實話,我一直把自由存儲區等價於堆。而在Herb Sutter的《exceptional C++》中,明確指出了free store(自由存儲區) 與 heap(堆) 是有區別的。關於自由存儲區與堆是否等價的問題討論,大概就是從這裏開始的:

Free Store
The free store is one of the two dynamic memory areas, allocated/freed by new/delete. Object lifetime can be less than the time the storage is allocated; that is, free store objects can have memory allocated without being immediately initialized, and can be destroyed without the memory being immediately deallocated. During the period when the storage is allocated but outside the object‘s lifetime, the storage may be accessed and manipulated through a void* but none of the proto-object‘s nonstatic members or member functions may be accessed, have their addresses taken, or be otherwise manipulated.

Heap
The heap is the other dynamic memory area, allocated/freed by malloc/free and their variants. Note that while the default global new and delete might be implemented in terms of malloc and free by a particular compiler, the heap is not the same as free store and memory allocated in one area cannot be safely deallocated in the other. Memory allocated from the heap can be used for objects of class type by placement-new construction and explicit destruction. If so used, the notes about free store object lifetime apply similarly here.

作者也指出,之所以把堆與自由存儲區要分開來,是因為在C++標準草案中關於這兩種區域是否有聯系的問題一直很謹慎地沒有給予詳細說明,而且特定情況下new和delete是按照malloc和free來實現,或者說是放過來malloc和free是按照new和delete來實現的也沒有定論。這兩種內存區域的運作方式不同、訪問方式不同,所以應該被當成不一樣的東西來使用。

結論

  • 自由存儲是C++中通過new與delete動態分配和釋放對象的抽象概念,而堆(heap)是C語言和操作系統的術語,是操作系統維護的一塊動態分配內存。

  • new所申請的內存區域在C++中稱為自由存儲區。藉由堆實現的自由存儲,可以說new所申請的內存區域在堆上。

  • 堆與自由存儲區還是有區別的,它們並非等價。

假如你來自C語言,從沒接觸過C++;或者說你一開始就熟悉C++的自由儲存概念,而從沒聽說過C語言的malloc,可能你就不會陷入“自由存儲區與堆好像一樣,好像又不同”這樣的迷惑之中。這就像Bjarne Stroustrup所說的:

usually because they come from a different language background.

大概只是語言背景不同罷了。

轉載:http://www.linuxidc.com/Linux/2015-12/126520.htm

【轉載】C++自由存儲區是否等價於堆