当向量被分配时,它们使用堆或堆栈上的内存吗? [英] When vectors are allocated, do they use memory on the heap or the stack?
本文介绍了当向量被分配时,它们使用堆或堆栈上的内存吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
以下所有语句是否真实?
Are all of the following statements true?
vector<Type> vect; //allocates vect on stack and each of the Type (using std::allocator) also will be on the stack
vector<Type> *vect = new vector<Type>; //allocates vect on heap and each of the Type will be allocated on stack
vector<Type*> vect; //vect will be on stack and Type* will be on heap.
内部分配的内存类型
在向量
或任何其他STL容器中?
How is the memory allocated internally for Type
in a vector
or any other STL container?
推荐答案
vector<Type> vect;
将分配向量
info,而是在自由存储(heap)上的元素。
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;
将在堆栈上分配向量
在自由存储上的一堆指针,但是这些点是由你如何使用它们(你可以将元素0指向自由存储和元素1指向堆栈,说)决定。
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).
这篇关于当向量被分配时,它们使用堆或堆栈上的内存吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文