日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

34.动态数组

發(fā)布時間:2025/3/21 编程问答 11 豆豆
生活随笔 收集整理的這篇文章主要介紹了 34.动态数组 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

new和delete運算符一次只分配/釋放一個對象,但是某些應用需要以此為很多對象分配內(nèi)存的功能。

C++中制定了兩種分配/釋放多個對象的機制!分別是:new[]/delete[]? 和 allocator類。

其中new[]/delete[]?再分配內(nèi)存的時候就初始化對象;

allocator類將內(nèi)存的分配和對象的初始化分離。

類似于預加載和懶加載。


new[]的初始化

int * p = new int[get_size()]; int * q = new int[100];//默認初始化,100個未初始化的int int * s = new int[100](); //值初始化,100個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是值初始化)

花括號列表形式的初始化器用來初始化動態(tài)數(shù)組的開始部分元素。如果初始化器給出的元素大于總的元素數(shù)目,則new表達是會出錯,不會分配任何內(nèi)存。當小于總的元素數(shù)目的時候,后面的剩余元素將會進行值初始化。

delete[]?釋放動態(tài)數(shù)組

typedef arrT[100] int ; int * p = new arrT; delete[] p;

當我們釋放一個指向數(shù)組的指針的時候,空括號是必須的。因為:它指示編譯器此指針指向一個對象數(shù)組的第一個元素!如果我們忽略了方括號,其行為是未定義的。

數(shù)組的元素的銷毀是逆序的。最后一個元素首先被銷毀,然后是倒數(shù)第二個,以此類推。


智能指針和動態(tài)數(shù)組

unique_ptr

unique_ptr<int[]> p(new int[100]()); //分配 p.reset() ; //釋放

unique_ptr使用下標訪問數(shù)組中的元素

unique_ptr<int[]>up(new int[10]); for(size_t i = 0;i != 10;i++) {up[i] = i; //使用下標進行賦值操作 }

shared_ptr 未定義下標運算符,不能通過下標來訪問數(shù)組元素。?因此是不支持管理一個動態(tài)數(shù)組的。但是如果我們需要進行這種操作就必須要給它傳遞一個刪除器;

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

其實很簡單,將內(nèi)存的創(chuàng)建+對象的構造分離開來;也就是將 對象的析構+內(nèi)存的釋放分離開來!

內(nèi)存創(chuàng)建:allocate()?? 然后返回第一個元素的起始地址

對象的構造:construct()?? 參數(shù)一 位置指針,參數(shù)二 對象

對象的析構:destory()? 傳遞指針

內(nèi)存釋放:deallocate()? //傳遞給deallocate的指針必須是之前某次allocate返回的指針,因此在deallocate之前我們必須要檢查該指針是否為空


舉個栗子:

#include <iostream> #include <memory>using namespace std;class Animal { public: #if 1 //即使為0,沒有默認構造也是可以,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);}//對象銷毀之后還可以繼續(xù)構建,因為構建和內(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ù)字化實踐50位技術專家共同創(chuàng)作,文字、視頻、音頻交互閱讀

總結

以上是生活随笔為你收集整理的34.动态数组的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。