34.动态数组
new和delete運(yùn)算符一次只分配/釋放一個(gè)對(duì)象,但是某些應(yīng)用需要以此為很多對(duì)象分配內(nèi)存的功能。
C++中制定了兩種分配/釋放多個(gè)對(duì)象的機(jī)制!分別是:new[]/delete[]? 和 allocator類。
其中new[]/delete[]?再分配內(nèi)存的時(shí)候就初始化對(duì)象;
allocator類將內(nèi)存的分配和對(duì)象的初始化分離。
類似于預(yù)加載和懶加載。
new[]的初始化
int * p = new int[get_size()]; int * q = new int[100];//默認(rèn)初始化,100個(gè)未初始化的int int * s = new int[100](); //值初始化,100個(gè)0 int * m = new int[10]{1,2,3,4,5,6,7,8,9}; //1,2,3,4,5,6,7,8,9,0(0是值初始化)花括號(hào)列表形式的初始化器用來初始化動(dòng)態(tài)數(shù)組的開始部分元素。如果初始化器給出的元素大于總的元素?cái)?shù)目,則new表達(dá)是會(huì)出錯(cuò),不會(huì)分配任何內(nèi)存。當(dāng)小于總的元素?cái)?shù)目的時(shí)候,后面的剩余元素將會(huì)進(jìn)行值初始化。
delete[]?釋放動(dòng)態(tài)數(shù)組
typedef arrT[100] int ; int * p = new arrT; delete[] p;當(dāng)我們釋放一個(gè)指向數(shù)組的指針的時(shí)候,空括號(hào)是必須的。因?yàn)?#xff1a;它指示編譯器此指針指向一個(gè)對(duì)象數(shù)組的第一個(gè)元素!如果我們忽略了方括號(hào),其行為是未定義的。
數(shù)組的元素的銷毀是逆序的。最后一個(gè)元素首先被銷毀,然后是倒數(shù)第二個(gè),以此類推。
智能指針和動(dòng)態(tài)數(shù)組
unique_ptr
unique_ptr<int[]> p(new int[100]()); //分配 p.reset() ; //釋放unique_ptr使用下標(biāo)訪問數(shù)組中的元素
unique_ptr<int[]>up(new int[10]); for(size_t i = 0;i != 10;i++) {up[i] = i; //使用下標(biāo)進(jìn)行賦值操作 }shared_ptr 未定義下標(biāo)運(yùn)算符,不能通過下標(biāo)來訪問數(shù)組元素。?因此是不支持管理一個(gè)動(dòng)態(tài)數(shù)組的。但是如果我們需要進(jìn)行這種操作就必須要給它傳遞一個(gè)刪除器;
shared_ptr<int>sp(new int[10],[](int* P){delete[] p;}); sp.reset(); // 使用lambda作為刪除器,釋放數(shù)組shared_ptr? 使用get()訪問數(shù)組元素;
for(size_t i = 0;i != 10;++i) {*(sp.get()+ i) = i; //通過get()返回內(nèi)置指針,訪問數(shù)組元素 }?
allocator類??? primer p428
其實(shí)很簡單,將內(nèi)存的創(chuàng)建+對(duì)象的構(gòu)造分離開來;也就是將 對(duì)象的析構(gòu)+內(nèi)存的釋放分離開來!
內(nèi)存創(chuàng)建:allocate()?? 然后返回第一個(gè)元素的起始地址
對(duì)象的構(gòu)造:construct()?? 參數(shù)一 位置指針,參數(shù)二 對(duì)象
對(duì)象的析構(gòu):destory()? 傳遞指針
內(nèi)存釋放:deallocate()? //傳遞給deallocate的指針必須是之前某次allocate返回的指針,因此在deallocate之前我們必須要檢查該指針是否為空
舉個(gè)栗子:
#include <iostream> #include <memory>using namespace std;class Animal { public: #if 1 //即使為0,沒有默認(rèn)構(gòu)造也是可以,Animal() : num(0){cout << "Animal constructor default" << endl;} #endifAnimal(int _num) : num(_num){cout << "Animal constructor param" << endl;}~Animal(){cout << "Animal destructor" << endl;}void show(){cout << this->num << endl;}private:int num; };int main() {allocator<Animal> alloc; //1.Animal *a = alloc.allocate(5); //2.//3.alloc.construct(a, 1);alloc.construct(a + 1);alloc.construct(a + 2, 3);alloc.construct(a + 3);alloc.construct(a + 4, 5);//4.a->show();(a + 1)->show();(a + 2)->show();(a + 3)->show();(a + 4)->show();//5.for (int i = 0; i < 5; i++){alloc.destroy(a + i);}//對(duì)象銷毀之后還可以繼續(xù)構(gòu)建,因?yàn)闃?gòu)建和內(nèi)存的分配是分離的//6.alloc.deallocate(a, 5);cin.get();return 0; }輸出
Animal constructor param Animal constructor default Animal constructor param Animal constructor default Animal constructor param 1 0 3 0 5 Animal destructor Animal destructor Animal destructor Animal destructor Animal destructor?
?
?
?
?
?
?
?
?
?
?
?
《新程序員》:云原生和全面數(shù)字化實(shí)踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀總結(jié)
- 上一篇: 主机到中继地址的发包路径
- 下一篇: 36.需要析构函数的类也需要拷贝和赋值操