Memory Management
Use the same form in corresponding uses of new and delete
eryar@163.com
對(duì)應(yīng)的new和delete要采用相同的形式
用new operator時(shí)會(huì)發(fā)生兩件事:首先,內(nèi)存通過operator new被分配;然后,為被分配的內(nèi)存調(diào)用一個(gè)或多個(gè)構(gòu)造函數(shù)。
用delete operator時(shí)也會(huì)發(fā)生兩件事:首先,為將釋放的內(nèi)存調(diào)用一個(gè)或多個(gè)析構(gòu)函數(shù);然后,通過operator delete釋放內(nèi)存。
對(duì)于delete operator來說會(huì)有這樣一個(gè)重要的問題:內(nèi)存中有多少個(gè)對(duì)象需要被刪除呢?刪除多了,程序也就崩潰了;刪除少了,就有內(nèi)存未被正確釋放。這個(gè)問題簡(jiǎn)單來說就是:要被刪除的指針指向是單個(gè)對(duì)象,還是對(duì)象數(shù)組?這只有你來告訴delete operator。如果你在用delete operator時(shí)沒有括號(hào),delete就會(huì)認(rèn)為指向的是單個(gè)對(duì)象;否則它就會(huì)認(rèn)為指向的是一個(gè)數(shù)組。
Prefer new and delete to malloc and free
malloc和free(及其變體)會(huì)產(chǎn)生問題的原因在于它們太簡(jiǎn)單:他們不知道構(gòu)造函數(shù)和析構(gòu)函數(shù)。(有時(shí)越簡(jiǎn)單越好。)
假設(shè)用兩種方法給一個(gè)包含10個(gè)string對(duì)象的數(shù)組分配空間,一個(gè)用malloc,另一個(gè)用new:
其結(jié)果是,stringarray1確實(shí)指向的是可以容納10個(gè)string對(duì)象的足夠空間,但內(nèi)存里并沒有創(chuàng)建這些對(duì)象。當(dāng)釋放這些內(nèi)存時(shí),你一定會(huì)這么做:
調(diào)用free將會(huì)釋放stringarray1指向的內(nèi)存,但內(nèi)存里的string對(duì)象不會(huì)調(diào)用析構(gòu)函數(shù)。如果string對(duì)象象一般情況那樣,自己已經(jīng)分配了內(nèi)存,那這些內(nèi)存將會(huì)全部丟失。相反,當(dāng)對(duì)stringarray2使用delete時(shí),數(shù)組里的每個(gè)string對(duì)象都會(huì)在內(nèi)存釋放前調(diào)用析構(gòu)函數(shù)。即然new和delete可以這么有效地與構(gòu)造函數(shù)和析構(gòu)函數(shù)交互,選用它們是顯然的。
把new/delete與malloc/free混用也是個(gè)壞想法。對(duì)一個(gè)用new獲取來的指針調(diào)用free,或者對(duì)一個(gè)用malloc獲取來的指針調(diào)用delete,其后果是不可預(yù)測(cè)的。
示例程序:
1:
2: #include <iostream>
3: using namespace std;
4:
5: class CTest
6: {
7: public:
8: CTest() { cout<<"Default constructor."<<endl; }
9: ~CTest() { cout<<"Default destrcutor."<<endl; }
10:
11: };
12:
13: int main(int argc, char *argv[])
14: {
15: cout<<"=== Test new/delete begin ==="<<endl;
16: CTest* pTestNewDelete = new CTest;
17: delete pTestNewDelete;
18: cout<<"=== Test new/delete end ==="<<endl;
19:
20: cout<<"~~~ Test malloc/free begin ~~~"<<endl;
21: CTest* pTestMallocFree = static_cast<CTest*> (malloc(sizeof(CTest)));
22: free(pTestMallocFree);
23: cout<<"~~~ Test malloc/free end ~~~"<<endl;
24:
25: return 0;
26: }
27:
輸出:
1 === Test new/delete begin ===
2 Default constructor.
3 Default destrcutor.
4 === Test new/delete end ===
5 ~~~ Test malloc/free begin ~~~
6 ~~~ Test malloc/free end ~~~
7
來源:
1. Scott Meyers Effective C++