vector在堆上還是在棧上(等待自己慢慢領悟吧)


探索1:

ME:: L, vector變量是存放在堆上還是棧上的?網上說法不一,我也沒有找到權威的表述
L: 堆、棧 和vector有啥關系?
L: 你把它放哪里,它就在哪里
ME: [分享]堆、棧的地址高低? 棧的增長方向? https://www.zhihu.com/question/36103513?sort=created
ME: 這個例子作者認為vector在堆上.
L: vector管理的內存在堆上
L: vector對象本身在棧上
L: 你把它放哪里,它就在哪里
ME: 那他的a3[1]還是在堆上嗎
L: 在
ME: 那a3是在棧上吧?
L: 在棧上定義的對象,就在棧上
ME: OK

 

不知哪里來的說法:

無論你的定義是:
vector<int*> *p = new vector<int*>;
還是
vector<int*> p;
其元素都是在堆上進行分配。

 

別的論述:

https://www.it1352.com/454497.html

vector<Type> vect;

will allocate the vector, i.e. the header info, on the stack, but the elements on the free store ("heap").

vector<Type> *vect = new vector<Type>;

allocates everything on the free store.

vector<Type*> vect;

will allocate the vector on the stack and a bunch of pointers on the free store, but where these point is determined by how you use them (you could point element 0 to the free store and element 1 to the stack, say).


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM