C++中內(nèi)存的動態(tài)分配與管理永遠是一個讓C++開發(fā)者頭痛的問題,本文通過對C++中內(nèi)存的動態(tài)分配釋放的基本原理的介紹,讓讀者朋友能對C++中的內(nèi)存的動態(tài)分配與釋放有較為深入的理解,從而更好駕馭C++程序。
1. 函數(shù)(Function)
(1) operator new function
void * ::operator new(size_t); //Global
void * class-name::operator new(size_t); //Class
上面是C++中operator new function的原型,一個是全局類型的,一個的類成員類型的。全局類型的operator new函數(shù)在下面兩種情況下被調(diào)用:一種是在分配C++內(nèi)建(built-in)類型的動態(tài)內(nèi)存時,一種是在分配用戶沒有自己定義operator new成員函數(shù)的用戶自定義類型的動態(tài)內(nèi)存時。 如果用戶在自己定義的類型中,定義了operator new函數(shù),那么用戶在用new申請該類型的動態(tài)內(nèi)存時, 便會調(diào)用該類型的成員函數(shù)operator new, 而不是全局的operator new.
另外,我們注意到,上面的原型中函數(shù)的返回值為void *類型, 第一個參數(shù)為size_t類型,這個是C++編譯器要求的,如果要自己重載operator new函數(shù),返回值必須為void* 類型,第一個參數(shù)必須為size_t類型,否則,編譯器會返回如下錯誤信息:
error: 'operator new' takes type 'size_t' ('unsigned int') as first parameter
這里需要注意的一點是,我們可以利用operator new function可以重載的特點,可以通過參數(shù)傳入一些額外的信息,來調(diào)試程序,檢測內(nèi)存泄露等。比如,我們可以像下面這樣重載,傳入調(diào)用處的行號,函數(shù)名,這樣就可以跟蹤內(nèi)存的分配使用情況:
void * operator new(size_t unSize, int nLine, const char * pFunc)
{
prinft("Line: %d, Func: %s, allocate %u byte(s)\n", nLine, pFunc, unSize);
return malloc(unSize);
}
(2) operator delete function
void operator delete( void * );
void operator delete( void *, size_t );
上面是operator delete function的原型。operator delete function也有全局的和類成員的兩種。這里需要注意,一個類只能有一個operator delete function做為其成員函數(shù),而且必須為上面兩種中的其中一種,沒有其它的形式。如果一個類實現(xiàn)了自己的operator delete function成員函數(shù),那么在釋放該類型的內(nèi)存時,編譯器便會調(diào)用成員operator delete function, 而不是全局的。
上面的兩種原型,第一種,在調(diào)用的時候,編譯器會把要釋放的內(nèi)存的首地址傳入,第二種,在調(diào)用的時候,編譯器會把要釋放的內(nèi)存的首地址和大小都傳入。因此,可以利用這一特性,如果我們在基類中實現(xiàn)第二種形式的operator delete function的成員函數(shù),那么便可以用之來釋放子類類型的內(nèi)存(具體參考最后面的例子)。
2. 運算符(Operator)
(1) new operator
[::] new [placement] new-type-name [new-initializer]
[::] new [placement] ( type-name ) [new-initializer]
注:上面的'[]'表示在其中的部分是optional(可選的)
上面是new operator的原型。在C++中,動態(tài)內(nèi)存的分配,通常都是調(diào)用new operator來完成的,利用new operator來分配動態(tài)內(nèi)存,編譯器要做下面兩項工作:
a. 調(diào)用operator new function分配內(nèi)存(allocate the memory)
b. 調(diào)用構(gòu)造函數(shù)(call the constructor)來進行初始化
下面來說一說new operator的原型中各部分到底是干什么的:
placement: 如果你重載了operator new function, placement可以用來傳遞額外的參數(shù)
type-name: 指定要分配的內(nèi)存的類型,可以是內(nèi)建(built-in)類型,也可以是用戶自定義類型
new-initializer: 指定對分配后內(nèi)存的初始化的參數(shù),也就的構(gòu)造函數(shù)的參數(shù) .這里需要注意一點,在分配一個對象的數(shù)組類型的內(nèi)存時,不能夠指定初始化參數(shù);換言之,要想分配一個對象的數(shù)組類型的內(nèi)存,該對象必須有缺省構(gòu)造函數(shù)
雅思答案 tygj123.com (2) delete opeartor
[::] delete cast-expression
[::] delete [ ] cast-expression
上面是delete operator的原型,第一種用來釋放普通的對象(包括內(nèi)建類型)類型的內(nèi)存,第二種用來釋放對象的數(shù)組類型的內(nèi)存。在C++中,用new operator分配的動態(tài)內(nèi)存,必須調(diào)用delete operator來釋放,通常用delete operator釋放內(nèi)存編譯器要做下面兩項工作:
a. 調(diào)用對象析構(gòu)函數(shù)來析構(gòu)對象
b. 調(diào)用operator delete function來釋放內(nèi)存(deallocate the memory)
3. 關(guān)于new/delete使用過程中一些需要注意的點
(1)如何區(qū)別operator new/delete function 與 new/delete operator ?
通過上面的講述,不難看出,我們分配/釋放動態(tài)內(nèi)存,調(diào)用的是new/delete operator, 而在調(diào)用new/delete的過程中,編譯器會自動調(diào)用operator new/delete function來完成實際的內(nèi)存分配/釋放的工作
(2) 用delete operator去釋放一塊不是由new operator釋放的內(nèi)存,結(jié)果是不可預料的,因此,切記,operator new與operator delete一定要配對使用,這是寫好程序的基礎(chǔ)
(3) new operator調(diào)用失敗會拋出std::bad_alloc異常,前提是你沒有自己重載對應的operator new function;delete operator失敗,常見的原因是多次delete同一塊內(nèi)存
(4) 如果一塊內(nèi)存被delete后,再對它解引用(Dereference),結(jié)果也是不可預測的,很可能導致程序崩潰
(5) delete一個空(NULL)指針是安全的,沒有任何害處的
(6) 類成員類型的operator new/delete函數(shù)必須為靜態(tài)(static)函數(shù),因此它們不能為虛函數(shù)(virtual function),也遵守public, protected, private的訪問權(quán)限控制
4. 關(guān)于上面所講的內(nèi)容的一些例子:
程序:
#include <stdio .h>
#include <stdlib .h>
void * operator new(size_t unSize)
{
printf("operator new called\n");
return malloc(unSize);
}
void * operator new(size_t unSize, int nLine, const char * pFunc)
{
printf("operator new called, line: %d, func: %s\n",
nLine, pFunc);
return malloc(unSize);
}
void operator delete(void * pMem)
{
printf("delete1\n");
free(pMem);
}
class A
{
public:
A(int a = 0) :
_a(a)
{
printf("constructor called\n");
}
{
printf("~A()\n");
}
static void operator delete(void * pMem, size_t unSize)
{
printf("delete2: %u\n", unSize);
free(pMem);
}
private:
int _a;
};
class B: public A
{
public:
~B()
{
printf("~B()\n");
}
int _b;
int _bb;
};
int main()
{
A * pA = new A(10);
printf("#######\n");
A * pB = new (__LINE__, __func__) B();
printf("#######\n");
A * szA = new A[10];
printf("#######\n");
delete pA;
printf("#######\n");
delete pB;
printf("#######\n");
delete [] szA;
printf("#######\n");
char * pC = NULL;
delete pC;
}
運行結(jié)果:
wuzesheng@wuzesheng-ubuntu:~/Program$ ./a.out
operator new called
constructor called
#######
operator new called, line: 68, func: main
constructor called
#######
operator new called
constructor called
constructor called
constructor called
constructor called
constructor called
constructor called
constructor called
constructor called
constructor called
constructor called
#######
~A()
delete2: 8
#######
~B()
~A()
delete2: 16
#######
~A()
~A()
~A()
~A()
~A()
~A()
~A()
~A()
~A()
~A()
delete1
#######
delete1
上面的程序很簡單,我在這里不做過多的解釋,感興趣的朋友可以自己分析一下。
通過我上面的講解,相信大多數(shù)朋友應該對C++中內(nèi)存的動態(tài)分配與釋放有了較為深入的理解。后續(xù)我還有可能寫一些關(guān)于C++中內(nèi)存管理的文章,只有把本文所講的內(nèi)容與后續(xù)的內(nèi)存管理的一些常見的方法結(jié)合起來,我們才寫出更加健壯的C++程序。
www.yz-lc.com