<四>理解空間配置器allocator, 優化STL 中的Vector

来源:https://www.cnblogs.com/erichome/archive/2022/11/20/16906631.html
-Advertisement-
Play Games

.在上一節我們實現的 MyVector存在哪些問題? 問題1 現在有Student類 class Student{ public: Student(){cout<<"構造Student對象"<<endl;} ~Student(){cout<<"析構Student對象"<<endl;} private ...


.在上一節我們實現的 MyVector存在哪些問題?

問題1

現在有Student類
class Student{
   public:
   Student(){cout<<"構造Student對象"<<endl;}
   ~Student(){cout<<"析構Student對象"<<endl;}
   private:
       int   age
       char  NAME[20];
       char  *pAddress;
}

MyVector<Student> v1[1000];

我只是希望創建一個能放1000個Student 的Vector,但是開始並不放任何內容,但是發現編譯器除分配了1000個student對象的空間,還創建了1000個對象,
在main函數結束後再析構這1000個對象,這就是問題1,這1000個對象並不是我需要的,
原因如下,在MyVector的構造函數中 T * _tep = new T[size](); 這個new除了分配空間,還會調用對象的構造函數完成對象的初始化
換句話說就是 空間的分配和對象的創建聯繫在了一起,這個非常不合理,我們需要把他分開,我希望是需要幫我開闢空間即可,不希望幫我創建1000個對象

問題2關於析構函數

//析構函數
~MyVector<T>() {
	if (Empty() == false) {
		delete[]first;
		first = nullptr;
		last  = nullptr;
		end   = nullptr;
		cout << "析構Vector,堆地址"<<first << endl;
	}

}

實際情況時,我的vector是可以放1000個student對象的容器,但是實際裡面可能暫時只放了100個對象,
而delete[] first,是把整個vector中的1000個student對象都調用析構函數,這也不合理,我們只需要析構有效
數量的對象然後再釋放first指針指向的堆記憶體

問題3 關於添加元素和刪除元素

student s1;
student s2;
student s3;

MyVector<student> v1(1000);// 這一句編譯器會先分配1000個student對象空間,然後再調用1000次構造函數創建1000個student對象


v1.pushback(s1);
//上面這句話會調用賦值函數,會將s1的內容賦值給已經在堆上的student對象,但實際上我想要的是,只需要將分配好的空間
//給我,我使用拷貝構造,將s1拷貝構造出來一個對象放在你給的空間上,這是個問題


v1.popBack(){
    this->last--;
}
//上面的pop最後一個studnet對象,只是將最後一個元素的指針前移動一個student空間大小,並沒有去析構這個對象,
//而且這個彈出的student對象裡面有一個char *pAddress 指向了外部堆記憶體資源,這個記憶體自己沒有去釋放的話,
//造成了記憶體泄漏了. 這個也是個問題,所以說我們要去析構這個彈出的student對象但是不用使用 delete 方式 delete 這個對象,為什麼呢?
//應為delete 處理調用析構函數完,他還要釋放記憶體空間,但是這個空間屬於Vector的,釋放掉了這一小塊空間,我們後面就無法在使用了,
//**所以一句話就是我們從vector中刪除一個對象時,只做析構這個對象而不釋放他的記憶體空間,把對象的析構和記憶體的釋放分開**

以上三個問題使得我們不能採用上一篇文字中的方式寫Vector,這就是引入了容器的空間配置器的原因

容器的空間配置器做了4件事
記憶體開闢/記憶體釋放 對象創建/對象析構

在上一遍的基礎上加入空間配置器,代碼如下

#include <iostream>
using namespace std;


class student {

public:

	student(int _age, char * _pname): 
		age(_age),
		pName(_pname){  
	}

	student(const student & _rvalue) {
		this->age = _rvalue.age;
		this->pName = new char[20];
		strcpy(this->pName, _rvalue.pName);
	}

	student & operator =(const student & _rvalue) {
		
		if (this == &_rvalue) { return *this; }
		
		delete[]this->pName;
		this->pName = nullptr;

		this->age = _rvalue.age;	
		this->pName = new char[20];
		strcpy(this->pName, _rvalue.pName);
	}
	~student() {
		delete[] this->pName;
		this->pName == nullptr;
		cout << "student 析構函數被執行,當前對象地址是" << this << endl;
	}


private:
	int age;
	char *pName;
};


template<typename T>
class MyAllocate {

public:

	T * allocate(int size) {
		return (T *)malloc(sizeof(T)*size);
	}

	//根據指定的地址,釋放記憶體空間
	void delAllocate(T *p) {
		free(p);
	}

	//在p指針指定的位置,根據指定的對象創建新對象
	void construct(T * p, const T & _rValue) {
		new (p) T(_rValue);
	}

	//析構指定對象,但不釋放記憶體空間
	void destory(T * p) {
		if (p != nullptr) {
			p->~T();
		}
	}



};



template<typename T, typename Allocate=MyAllocate<T>>
class MyVector2 {

public:

	//構造函數
	MyVector2<T, Allocate>(int size = 10, const Allocate & _allocate = MyAllocate<T>()) : allocator(_allocate) {
		
		first = allocator.allocate(size);
		last = first;
		end = first + size;
		cout << "MyVector2 構造函數,構建數量=" << size <<"堆空間構造起始地址="<<first<<"結束地址=" << endl;
	}

	//拷貝構造,根據指定的 MyVector2 創建新的MyVector2
	MyVector2<T, Allocate>(const MyVector2<T,Allocate> & _rValue) {
		
		//1:根據原MyVector2的Size 申請記憶體空間
		first = allocator.allocate(_rValue.Size());
		last = first;
		end = first + __rValue.Size();

		//2:根據原MyVector2內的對象,在第1步申請的堆記憶體中構造對象
		T *tep = _rValue.first;
		while (tep<_rValue.end)
		{			
			allocate.construct(last, *tep)
			last++;
			tep++;
		}
		cout << "MyVector2 拷貝構造函數,構建數量" << __rValue.Size() << endl;
	}

	//賦值函數
	MyVector2<T, Allocate> & operator=(const MyVector2<T, Allocate> & _rValue) {
		if (this == &_rValue) { return *this;}

		//1:先析構目標Vector2中所有的對象
		T * tep = first;
		while (tep < last) {
			allocator.destory(tep);
			tep++;
		}

		//2:釋放目標Vector2的記憶體空間
		allocator.delAllocate(first);

		//3:根據原MyVector2的size 申請新的記憶體空間
		int rSize = _rValue.Size();
		T * _head allocator.allocate(rSize);
		first = _head;
		last  = _head;
		end   = first + rSize;

		//4:根據原MyVector2中的有效的對象在 第3步申請的記憶體空間上構建對象

		T *tep = _rValue.first;
		while (tep<_rValue.end)
		{
			allocator.construct(last, *tep)
			last++;
			tep++;
		}
	
		cout << "MyVector2 賦值函數,構建數量" << rSize << endl;

	}
	
	//在已經申請空間的位置 添加值
	void pushBack(const T & _addValue) {
		if (Full()) {
			Expend();//兩倍擴容
		}
		//在指定地址空間 構建對象
		allocator.construct(last, _addValue);
		cout << "pushBack 元素,記憶體地址=" << last << endl;
		last++;
		
	}

	//彈出
	void popBack() {
		if (Empty()) { return; }
		//1:只析構指定位置對象,但不釋放空間
		allocator.destory(last - 1);
		cout << "popBack元素,其記憶體地址=" << (last - 1) << endl;

		//2:移動last指針
		last--;
	
	}
	
	//是否為空
	bool Empty() { return first == last; }
	
	//是否滿
	bool Full() { return  last  == end;  }

	int  Size() { return   end - first; }

	//容器擴容
	void Expend() {

		int newSize = this->Size() * 2;//兩倍擴容
		
		//1:申請新的空間
		T * head = allocator.allocate(newSize);
		T * tep_first = head;
		T * tep_last =  head;
		T * tep_end  = first + newSize;

		cout << "兩倍擴容,新的堆記憶體起始地址=" << head << endl;

		//2:將原來有效的對象 賦值到新的堆空間上
		T * tep = first;
		while (tep < last) {
			allocator.construct(tep_first,*tep);
			cout << "兩倍擴容,賦值對象,原對象地址=" << tep <<"目標對象地址="<<tep_first<< endl;
			tep_first++;	
			tep_last++;
			tep++;
		}

		tep = first;
		//3:析構原堆空間上有效的對象
		while (tep < last) {
			allocator.destory(tep);
			cout << "兩倍擴容,析構對象,對象地址=" << tep << endl;
			tep++;
			
		}

		//4:釋放堆上的空間
		allocator.delAllocate(first);
		cout << "兩倍擴容,銷毀原空間" << first << endl;
		first = head;
		last  = tep_last;
		end   = first + newSize;

	}


	void showVectorInfo() {
		T * tep = first;
		while (tep < last)
		{
			cout << "列印Vector中有效對象地址=" << tep << endl;
			tep++;
		}
	}


private:
	T * first;
	T * last;
	T * end;
	Allocate allocator;

};



int main() {

	MyVector2<student, MyAllocate<student>> v(4);

	student s1(20, "zs1");

	v.pushBack(s1);

	student s2(22, "zs2");

	v.pushBack(s2);

	student s3(23, "zs3");

	v.pushBack(s3);


	student s4(24, "zs4");

	v.pushBack(s4);

	v.showVectorInfo();


	student s5(25, "zs5");

	v.pushBack(s5);
	
	v.showVectorInfo();

	system("pause");

	return 1;
}

您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 一般,做資料庫表設計時,很多情況下,我們都至少需要一個日期欄位來記錄操作相關。 而這種表示日期的欄位,我們使用 datetime 類型比較多,但是如果使用 datetime 類型,直接前端拿日期時,我們會發現拿到日期格式是 2022-11-20T11:10:00.000+00:00 這種的,但我們一 ...
  • C++ 讀取文件及保留小數方法 做圖論作業時,需要從文件中讀取整型數據。之前都是在標準輸入輸出流中讀取和輸出。今小記一下。 讀取文件 使用文件流ifstream 最簡潔的方法是使用文件流: ifstream infile(filename) 假設 test.txt 文件中存放5 6: ifstrea ...
  • 一.小結 1.電腦是儲存和處理數據的電子設備 2.電腦包括硬體和軟體兩個部分 3.硬體是電腦中可以看見的物理部分 4.電腦程式,也就是通常所說的軟體,是一些看不見的指令,它們控制硬體完成任務 5.電腦程式設計就是編寫電腦執行的指令(即代碼) 6.中央處理器(CPU)是電腦的大腦。它是內 ...
  • 接上回,聊聊函子 functor。 functor 是一個容器。該容器的 value 屬性指向被包裹的數據;該容器的 map 方法對容器進行映射變換。 以下代碼實現一個最普通的 functor,稱之為 Just, 根據 map 的傳參 fn 對 value 進行變換: class Just<T> { ...
  • 指定ID 在類中聲明並定義按鈕控制項的起始ID,以控制項的類型和功能對動態控制項ID進行分組,每組最好定義一個自己的起始ID方便管理: #define IDC_CONTROL_START 1000 #define IDC_BTN_START IDC_CONTROL_START+100 #define ID ...
  • 以python 3為例關於迴圈中經常出現賦值問題的幾個形式(要賦值的變數a,迴圈變數b)就比如for i in range(n): 相對於b來說 1:a += b 是對每次b在迴圈過程中的值進行求和,每次迴圈中b與b之間沒有聯繫 2:b += b 是將每次b的值繼續帶入下一次迴圈中,會對下一次迴圈的 ...
  • 1.1 冪等性的概念 Methods can also have the property of "idempotence" in that (aside from error or expiration issues) the side-effects of N > 0 identical req ...
  • 遞歸方式基本思想: 1、當待處理節點非空時,判斷其左右孩子是否不同時為空:若是,轉到2、否則分別遞歸調用左右子樹進行操作。 2、新建一個輔助結點,執行交換操作。 3、遞歸調用非空的左右子樹進行操作。 BiTree *exchangeChild(BiTree *&T){ if(T==null) ret ...
一周排行
    -Advertisement-
    Play Games
  • Dapr Outbox 是1.12中的功能。 本文只介紹Dapr Outbox 執行流程,Dapr Outbox基本用法請閱讀官方文檔 。本文中appID=order-processor,topic=orders 本文前提知識:熟悉Dapr狀態管理、Dapr發佈訂閱和Outbox 模式。 Outbo ...
  • 引言 在前幾章我們深度講解了單元測試和集成測試的基礎知識,這一章我們來講解一下代碼覆蓋率,代碼覆蓋率是單元測試運行的度量值,覆蓋率通常以百分比表示,用於衡量代碼被測試覆蓋的程度,幫助開發人員評估測試用例的質量和代碼的健壯性。常見的覆蓋率包括語句覆蓋率(Line Coverage)、分支覆蓋率(Bra ...
  • 前言 本文介紹瞭如何使用S7.NET庫實現對西門子PLC DB塊數據的讀寫,記錄了使用電腦模擬,模擬PLC,自至完成測試的詳細流程,並重點介紹了在這個過程中的易錯點,供參考。 用到的軟體: 1.Windows環境下鏈路層網路訪問的行業標準工具(WinPcap_4_1_3.exe)下載鏈接:http ...
  • 從依賴倒置原則(Dependency Inversion Principle, DIP)到控制反轉(Inversion of Control, IoC)再到依賴註入(Dependency Injection, DI)的演進過程,我們可以理解為一種逐步抽象和解耦的設計思想。這種思想在C#等面向對象的編 ...
  • 關於Python中的私有屬性和私有方法 Python對於類的成員沒有嚴格的訪問控制限制,這與其他面相對對象語言有區別。關於私有屬性和私有方法,有如下要點: 1、通常我們約定,兩個下劃線開頭的屬性是私有的(private)。其他為公共的(public); 2、類內部可以訪問私有屬性(方法); 3、類外 ...
  • C++ 訪問說明符 訪問說明符是 C++ 中控制類成員(屬性和方法)可訪問性的關鍵字。它們用於封裝類數據並保護其免受意外修改或濫用。 三種訪問說明符: public:允許從類外部的任何地方訪問成員。 private:僅允許在類內部訪問成員。 protected:允許在類內部及其派生類中訪問成員。 示 ...
  • 寫這個隨筆說一下C++的static_cast和dynamic_cast用在子類與父類的指針轉換時的一些事宜。首先,【static_cast,dynamic_cast】【父類指針,子類指針】,兩兩一組,共有4種組合:用 static_cast 父類轉子類、用 static_cast 子類轉父類、使用 ...
  • /******************************************************************************************************** * * * 設計雙向鏈表的介面 * * * * Copyright (c) 2023-2 ...
  • 相信接觸過spring做開發的小伙伴們一定使用過@ComponentScan註解 @ComponentScan("com.wangm.lifecycle") public class AppConfig { } @ComponentScan指定basePackage,將包下的類按照一定規則註冊成Be ...
  • 操作系統 :CentOS 7.6_x64 opensips版本: 2.4.9 python版本:2.7.5 python作為腳本語言,使用起來很方便,查了下opensips的文檔,支持使用python腳本寫邏輯代碼。今天整理下CentOS7環境下opensips2.4.9的python模塊筆記及使用 ...