程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> 第18章 特殊工具與技術(2)

第18章 特殊工具與技術(2)

編輯:C++入門知識

上一篇:http://www.BkJia.com/kf/201201/116032.html

18.1.3 operator new函數和operator delete函數

當使用new表達式的時候,實際上發生了三個步驟。首先該表達式調用名為operator new的標准庫函數,分配足夠大的原始的未類型化的內存,以保存指定類型的一個對象;接下來,運行該類型的一個構造函數,用指定初始化式構造對象;最後,返回指向新分配並構造的對象的指針。

當使用delete表達式delete sp;刪除動態分配對象的時候,發生兩個步驟。首先,對sp指向的對象運行適當的析構函數;然後,通過調用名為operator delete的標准庫函數釋放該對象所用內存。

標准庫函數operator new和operator delete的命名容易讓人誤解。與其他opertaor函數(如operator=)不同,這些函數沒有重載new或delete表達式,實際上,我們不能重定義new和delete表達式的行為。

通過調用operator new函數執行new表達式獲得內存,並接著在該內存中構造一個對象,通過撤銷一個執行delete表達式,並接著調用operator delete函數,以釋放該對象使用的內存。

因為new(或delete)表達式與標准庫函數同名,所以二者容易混淆。

1. operator new和operator delete接口

operator new和operator delete函數有兩個重載版本,每個版本支持相關的new表達式和delete表達式。


void *operator new(size_t); //allocate an object  
void *operator new[](size_t); //allocate an array  
 
void operator delete(void*); //delete memory of an object  
void operator delete[](void*); //delete memory of an array 
void *operator new(size_t); //allocate an object
void *operator new[](size_t); //allocate an array

void operator delete(void*); //delete memory of an object
void operator delete[](void*); //delete memory of an array

2. 使用分配操作符函數

雖然operator new和operator delete函數的設計意圖是供new表達式使用,但它們通常是標准庫中的可用函數。可以使用它們獲得未構造內存,它們有點類似allocator類的allocate和deallocate成員。


template<class T> 
class Vector{ 
public: 
    Vector():elements(0),first_free(0),end(0){} 
    void push_back(const T&); 
    void *operator new[](size_t); //allocate an array  
    void operator delete[](void*); //delete memory of an array  
private: 
    static std::allocator<T> alloc; 
    void reallocate(); 
    T* elements; //first element  
    T* first_free; //behind the last actual element  
    T* end; //behind vector conent  
}; 
 
template<class T> 
void Vector<T>::push_back(const T& t){ 
if(first_free==end) 
    reallocate(); //gets more space and copies existing elements to it  
alloc.construct(first_free,t); 
++first_free; 

 
template <class T> 
void Vector<T>::reallocate(){ 
std::ptrdiff_t size=first_free-elements; 
std::ptrdiff_t newcapacity=2*max(size,1); 
T* newelements=static_cast<T*>(alloc.operator new[](newcapacity)); 
 
uninitialized_copy(elements,first_free,newelements); 
 
for(T *p=first_free;p!=elements;){ 
alloc.destroy(--p); 

 
if(elements) 
    alloc.operator delete[](elements,end-elements); 
 
elements=newelements; 
first_free=elements+size; 
end=elements=newcapacity; 

template<class T>
class Vector{
public:
 Vector():elements(0),first_free(0),end(0){}
 void push_back(const T&);
 void *operator new[](size_t); //allocate an array
 void operator delete[](void*); //delete memory of an array
private:
 static std::allocator<T> alloc;
 void reallocate();
 T* elements; //first element
 T* first_free; //behind the last actual element
 T* end; //behind vector conent
};

template<class T>
void Vector<T>::push_back(const T& t){
if(first_free==end)
 reallocate(); //gets more space and copies existing elements to it
alloc.construct(first_free,t);
++first_free;
}

template <class T>
void Vector<T>::reallocate(){
std::ptrdiff_t size=first_free-elements;
std::ptrdiff_t newcapacity=2*max(size,1);
T* newelements=static_cast<T*>(alloc.operator new[](newcapacity));

uninitialized_copy(elements,first_free,newelements);

for(T *p=first_free;p!=elements;){
alloc.destroy(--p);
}

if(elements)
 alloc.operator delete[](elements,end-elements);

elements=newelements;
first_free=elements+size;
end=elements=newcapacity;
}這些函數的表現與allocator類的allocate和deallocate成員類似。但是,它們在一個重要方面有不同:它們在void*指針而不是類型化的指針上進行操作。

一般而言,使用allocator比直接使用operator new和operator delete函數更為類型安全。

allocate成員分配類型化的內存,所以使用它的程序可以不必計算以字節為單位的所需內存量,它們也可以避免對operator new的返回值進行強制類型轉換。類似地,deallocate釋放特定類型的內存,也不必轉換為void*。

摘自 xufei96的專欄
 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved