C++全面覆蓋內(nèi)存管理知識講解
前言
C語言內(nèi)存管理方式在C++中可以繼續(xù)使用,但有些地方就無能為力而且使用起來比較麻煩,因此C++又提出了自己的內(nèi)存管理方式:通過new和delete操作符進行動態(tài)內(nèi)存管理。
一、C++內(nèi)存管理方式
1.1new/delete操作內(nèi)置類型
void Test() { // 動態(tài)申請一個int類型的空間 int* ptr4 = new int; // 動態(tài)申請一個int類型的空間并初始化為10 int* ptr5 = new int(10); // 動態(tài)申請10個int類型的空間 int* ptr6 = new int[3]; delete ptr4; delete ptr5; delete[] ptr6; }
注意:申請和釋放單個元素的空間,使用new和delete操作符,申請和釋放連續(xù)的空間,使用new[]和delete[]
二、operator new與operator delete函數(shù)
2.1operator new與operator delete函數(shù)
new和delete是用戶進行動態(tài)內(nèi)存申請和釋放的操作符,operator new 和operator delete是系統(tǒng)提供的全局函數(shù),new在底層調(diào)用operator new全局函數(shù)來申請空間,delete在底層通過operator delete全局函數(shù)來釋放空間。
/* operator new:該函數(shù)實際通過malloc來申請空間,當malloc申請空間成功時直接返回;申請空間失敗, 嘗試執(zhí)行空 間不足應(yīng)對措施,如果改應(yīng)對措施用戶設(shè)置了,則繼續(xù)申請,否則拋異常。 */ void *__CRTDECL operator new(size_t size) _THROW1(_STD bad_alloc) { // try to allocate size bytes void *p; while ((p = malloc(size)) == 0) if (_callnewh(size) == 0) { // report no memory // 如果申請內(nèi)存失敗了,這里會拋出bad_alloc 類型異常 static const std::bad_alloc nomem; _RAISE(nomem); } return (p); } /* operator delete: 該函數(shù)最終是通過free來釋放空間的 */ void operator delete(void *pUserData) { _CrtMemBlockHeader * pHead; RTCCALLBACK(_RTC_Free_hook, (pUserData, 0)); if (pUserData == NULL) return; _mlock(_HEAP_LOCK); /* block other threads */ __TRY /* get a pointer to memory block header */ pHead = pHdr(pUserData); /* verify block type */ _ASSERTE(_BLOCK_TYPE_IS_VALID(pHead->nBlockUse)); _free_dbg( pUserData, pHead->nBlockUse ); __FINALLY _munlock(_HEAP_LOCK); /* release other threads */ __END_TRY_FINALLY return; } /* free的實現(xiàn) */ #define free(p) _free_dbg(p, _NORMAL_BLOCK)
通過上述兩個全局函數(shù)的實現(xiàn)知道,operator new 實際也是通過malloc來申請空間,如果malloc申請空間成功就直接返回,否則執(zhí)行用戶提供的空間不足應(yīng)對措施,如果用戶提供該措施就繼續(xù)申請,否則就拋異常。operator delete 最終是通過free來釋放空間的。
二、new和delete的實現(xiàn)原理
2.1內(nèi)置類型
如果申請的是內(nèi)置類型的空間,new和malloc,delete和free基本類似,不同的地方是:new/delete申請和釋放的是單個元素的空間,new[]和delete[]申請的是連續(xù)空間,而且new在申請空間失敗時會拋異常,malloc會返回NULL。
2.2 自定義類型
new的原理
- 調(diào)用operator new函數(shù)申請空間
- 在申請的空間上執(zhí)行構(gòu)造函數(shù),完成對象的構(gòu)造
delete的原理
- 在空間上執(zhí)行析構(gòu)函數(shù),完成對象中資源的清理工作
- 調(diào)用operator delete函數(shù)釋放對象的空間
new T[N]的原理
- 調(diào)用operator new[]函數(shù),在operator new[]中實際調(diào)用operator new函數(shù)完成N個對象空間的申請
- 在申請的空間上執(zhí)行N次構(gòu)造函數(shù)
delete[]的原理
- 在釋放的對象空間上執(zhí)行N次析構(gòu)函數(shù),完成N個對象中資源的清理
- 調(diào)用operator delete[]釋放空間,實際在operator delete[]中調(diào)用operator delete來釋放空間
到此這篇關(guān)于C++全面覆蓋內(nèi)存管理知識講解的文章就介紹到這了,更多相關(guān)C++內(nèi)存管理內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
linux c++模擬簡易網(wǎng)絡(luò)爬蟲實例
下面小編就為大家?guī)硪黄猯inux c++模擬簡易網(wǎng)絡(luò)爬蟲實例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-06-06文件編譯時出現(xiàn)multiple definition of ''xxxxxx''的具體解決方法
以下是對文件編譯時出現(xiàn)multiple definition of 'xxxxxx'的解決方法進行了詳細的分析介紹,如也遇到此問題的朋友們可以過來參考下2013-07-07C++ Qt開發(fā)之ComboBox下拉組合框組件用法詳解
Qt 是一個跨平臺C++圖形界面開發(fā)庫,利用Qt可以快速開發(fā)跨平臺窗體應(yīng)用程序,在Qt中,ComboBox(組合框)是一種常用的用戶界面控件,它提供了一個下拉列表,允許用戶從預(yù)定義的選項中選擇一個,本文給大家介紹QComboBox類的一些常用方法,需要的朋友可以參考下2023-12-12