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.
如何在 vector 或任何其他 STL 容器中为 Type 内部分配内存?
How is the memory allocated internally for Type in a vector or any other STL container?
推荐答案
vector<Type> vect;
将在堆栈上分配vector,即头信息,但在空闲存储(堆")上分配元素.
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;
将在堆栈上分配 vector 并在空闲存储上分配一堆指针,但是这些点的位置取决于您如何使用它们(您可以将元素 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).
这篇关于分配向量时,它们使用堆上的内存还是堆栈上的内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:分配向量时,它们使用堆上的内存还是堆栈上的内存?
基础教程推荐
- 与 CAS 的原子交换(使用 gcc 同步内置函数) 2022-01-01
- 提升 ASIO 流缓冲 2021-01-01
- 如何部分禁用 cmake C/C++ 自定义编译器检查 2021-01-01
- 如何在 C++ 中正确使用命名空间? 2022-01-01
- c++ STL设置差异 2022-01-01
- 如何更改 SysDateTimePick32 或 CDateTimeCtrl 的背景颜色? 2022-01-01
- 随机插入/删除的综合向量与链表基准 2022-01-01
- C++:获取传递给函数的多维数组的行大小 2021-01-01
- 将不可复制的闭包对象传递给 std::function 参数 2021-01-01
- 为什么我们不能使用“虚拟继承"?在 COM 中? 2022-01-01
