C++ 自由存儲(chǔ)區(qū)是否等價(jià)于堆你知道嗎
free store” VS “heap”
當(dāng)我問(wèn)你C++的內(nèi)存布局時(shí),你大概會(huì)回答:
“在C++中,內(nèi)存區(qū)分為5個(gè)區(qū),分別是堆、棧、自由存儲(chǔ)區(qū)、全局/靜態(tài)存儲(chǔ)區(qū)、常量存儲(chǔ)區(qū)”。
如果我接著問(wèn)你自由存儲(chǔ)區(qū)與堆有什么區(qū)別,你或許這樣回答:
“malloc在堆上分配的內(nèi)存塊,使用free釋放內(nèi)存,而new所申請(qǐng)的內(nèi)存則是在自由存儲(chǔ)區(qū)上,使用delete來(lái)釋放?!?/p>
這樣聽(tīng)起來(lái)似乎也沒(méi)錯(cuò),但如果我接著問(wèn):
自由存儲(chǔ)區(qū)與堆是兩塊不同的內(nèi)存區(qū)域嗎?它們有可能相同嗎?
你可能就懵了。
事實(shí)上,我在網(wǎng)上看的很多博客,劃分自由存儲(chǔ)區(qū)與堆的分界線就是new/delete與malloc/free。然而,盡管C++標(biāo)準(zhǔn)沒(méi)有要求,但很多編譯器的new/delete都是以malloc/free為基礎(chǔ)來(lái)實(shí)現(xiàn)的。那么請(qǐng)問(wèn):借以malloc實(shí)現(xiàn)的new,所申請(qǐng)的內(nèi)存是在堆上還是在自由存儲(chǔ)區(qū)上?
從技術(shù)上來(lái)說(shuō),堆(heap)是C語(yǔ)言和操作系統(tǒng)的術(shù)語(yǔ)。堆是操作系統(tǒng)所維護(hù)的一塊特殊內(nèi)存,它提供了動(dòng)態(tài)分配的功能,當(dāng)運(yùn)行程序調(diào)用malloc()時(shí)就會(huì)從中分配,稍后調(diào)用free可把內(nèi)存交還。而自由存儲(chǔ)是C++中通過(guò)new和delete動(dòng)態(tài)分配和釋放對(duì)象的抽象概念,通過(guò)new來(lái)申請(qǐng)的內(nèi)存區(qū)域可稱(chēng)為自由存儲(chǔ)區(qū)?;旧?,所有的C++編譯器默認(rèn)使用堆來(lái)實(shí)現(xiàn)自由存儲(chǔ),也即是缺省的全局運(yùn)算符new和delete也許會(huì)按照malloc和free的方式來(lái)被實(shí)現(xiàn),這時(shí)藉由new運(yùn)算符分配的對(duì)象,說(shuō)它在堆上也對(duì),說(shuō)它在自由存儲(chǔ)區(qū)上也正確。但程序員也可以通過(guò)重載操作符,改用其他內(nèi)存來(lái)實(shí)現(xiàn)自由存儲(chǔ),例如全局變量做的對(duì)象池,這時(shí)自由存儲(chǔ)區(qū)就區(qū)別于堆了。我們所需要記住的就是:
堆是操作系統(tǒng)維護(hù)的一塊內(nèi)存,而自由存儲(chǔ)是C++中通過(guò)new與delete動(dòng)態(tài)分配和釋放對(duì)象的抽象概念。堆與自由存儲(chǔ)區(qū)并不等價(jià)。
問(wèn)題的來(lái)源
再回過(guò)頭來(lái)來(lái)看看這個(gè)問(wèn)題的起源在哪里。最先我們使用C語(yǔ)言的時(shí)候,并沒(méi)有這樣的爭(zhēng)議,很明確地知道m(xù)alloc/free是在堆上進(jìn)行內(nèi)存操作。直到我們?cè)贐jarne Stroustrup的書(shū)籍中數(shù)次看到free store (自由存儲(chǔ)區(qū)),說(shuō)實(shí)話,我一直把自由存儲(chǔ)區(qū)等價(jià)于堆。而在Herb Sutter的《exceptional C++》中,明確指出了free store(自由存儲(chǔ)區(qū)) 與 heap(堆) 是有區(qū)別的。關(guān)于自由存儲(chǔ)區(qū)與堆是否等價(jià)的問(wèn)題討論,大概就是從這里開(kāi)始的:
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.
來(lái)源:http://www.gotw.ca/gotw/009.htm
作者也指出,之所以把堆與自由存儲(chǔ)區(qū)要分開(kāi)來(lái),是因?yàn)樵贑++標(biāo)準(zhǔn)草案中關(guān)于這兩種區(qū)域是否有聯(lián)系的問(wèn)題一直很謹(jǐn)慎地沒(méi)有給予詳細(xì)說(shuō)明,而且特定情況下new和delete是按照malloc和free來(lái)實(shí)現(xiàn),或者說(shuō)是放過(guò)來(lái)malloc和free是按照new和delete來(lái)實(shí)現(xiàn)的也沒(méi)有定論。這兩種內(nèi)存區(qū)域的運(yùn)作方式不同、訪問(wèn)方式不同,所以應(yīng)該被當(dāng)成不一樣的東西來(lái)使用。
結(jié)論
- 自由存儲(chǔ)是C++中通過(guò)new與delete動(dòng)態(tài)分配和釋放對(duì)象的抽象概念,而堆(heap)是C語(yǔ)言和操作系統(tǒng)的術(shù)語(yǔ),是操作系統(tǒng)維護(hù)的一塊動(dòng)態(tài)分配內(nèi)存。
- new所申請(qǐng)的內(nèi)存區(qū)域在C++中稱(chēng)為自由存儲(chǔ)區(qū)。藉由堆實(shí)現(xiàn)的自由存儲(chǔ),可以說(shuō)new所申請(qǐng)的內(nèi)存區(qū)域在堆上。
- 堆與自由存儲(chǔ)區(qū)還是有區(qū)別的,它們并非等價(jià)。
本篇文章就到這里了,希望能夠給你帶來(lái)幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
opencv檢測(cè)直線方法之形態(tài)學(xué)方法
這篇文章主要為大家詳細(xì)介紹了opencv檢測(cè)直線方法之形態(tài)學(xué)方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12C語(yǔ)言實(shí)現(xiàn)Fibonacci數(shù)列遞歸
這篇文章主要介紹了C語(yǔ)言實(shí)現(xiàn)Fibonacci數(shù)列遞歸,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02C++中棧結(jié)構(gòu)建立與操作詳細(xì)解析
我們可以把棧理解成一個(gè)大倉(cāng)庫(kù),放在倉(cāng)庫(kù)門(mén)口(棧頂)的貨物會(huì)優(yōu)先被取出,然后再取出里面的貨物。而從數(shù)據(jù)的邏輯結(jié)構(gòu)來(lái)看,棧結(jié)構(gòu)起始就是一種線性結(jié)構(gòu)2013-10-10