C++ 分配向量时,它们使用堆上的内存还是堆栈上的内存?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8036474/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
When vectors are allocated, do they use memory on the heap or the stack?
提问by Phelodas
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.
How is the memory allocated internally for Type
in a vector
or any other STL container?
内存是如何在内部分配给Type
一个vector
或任何其他 STL 容器的?
回答by Fred Foo
vector<Type> vect;
will allocate the vector
, i.e. the header info, on the stack, but the elements on the free store ("heap").
将vector
在堆栈上分配,即头信息,但在空闲存储(“堆”)上分配元素。
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).
将分配vector
堆栈上的 和空闲存储上的一堆指针,但是这些点的位置取决于您如何使用它们(例如,您可以将元素 0 指向空闲存储,将元素 1 指向堆栈)。
回答by Flexo
Assuming an implementation which actually has a stack and a heap (standard C++ makes no requirement to have such things) the only true statement is the last one.
假设一个实现实际上有一个堆栈和一个堆(标准 C++ 不要求有这样的东西),唯一正确的陈述是最后一个。
vector<Type> vect;
//allocates vect on stack and each of the Type (using std::allocator) also will be on the stack
This is true, except for the last part (Type
won't be on the stack). Imagine:
这是真的,除了最后一部分(Type
不会在堆栈上)。想象:
void foo(vector<Type>& vec) {
// Can't be on stack - how would the stack "expand"
// to make the extra space required between main and foo?
vec.push_back(Type());
}
int main() {
vector<Type> bar;
foo(bar);
}
Likewise:
同样地:
vector<Type> *vect = new vector<Type>; //allocates vect on heap and each of the Type will be allocated on stack
True except the last part, with a similar counter example:
True 除了最后一部分,还有一个类似的反例:
void foo(vector<Type> *vec) {
// Can't be on stack - how would the stack "expand"
// to make the extra space required between main and foo?
vec->push_back(Type());
}
int main() {
vector<Type> *bar = new vector<Type>;
foo(bar);
}
For:
为了:
vector<Type*> vect; //vect will be on stack and Type* will be on heap.
this is true, but note here that the Type*
pointers will be on the heap, but the Type
instances they point to need not be:
这是真的,但请注意,Type*
指针将在堆上,但Type
它们指向的实例不必是:
int main() {
vector<Type*> bar;
Type foo;
bar.push_back(&foo);
}
回答by jpalecek
vector<Type> vect; //allocates vect on stack and each of the Type (using std::allocator) also will be on the stack
vector<Type> vect; //allocates vect on stack and each of the Type (using std::allocator) also will be on the stack
No, vect
will be on the stack, but the array it uses internally to store the items will be on the heap. The items will reside in that array.
不,vect
将在堆栈上,但它内部用于存储项目的数组将在堆上。这些项目将驻留在该数组中。
vector<Type> *vect = new vector<Type>; //allocates vect on heap and each of the Type will be allocated on stack
vector<Type> *vect = new vector<Type>; //allocates vect on heap and each of the Type will be allocated on stack
No. Same as above, except the vector
class will be on the heap as well.
不。与上面相同,除了vector
类也将在堆上。
vector<Type*> vect; //vect will be on stack and Type* will be on heap.
vector<Type*> vect; //vect will be on stack and Type* will be on heap.
vect
will be on the stack, its items (pointers to Type
) will be on the heap, and you can't tell where will be the Type
s the pointers point to. Could be on stack, could be on the heap, could be in the global data, could be nowhere (ie. NULL
pointers).
vect
将在堆栈上,它的项目(指向 的指针Type
)将在堆上,您无法确定Type
指针指向的s 在哪里。可能在堆栈上,可能在堆上,可能在全局数据中,可能不在任何地方(即NULL
指针)。
BTW the implementation could in fact store some vectors (typically of small size) on the stack entirely. Not that I know of any such implementation, but it can.
顺便说一句,该实现实际上可以将一些向量(通常是小尺寸的)完全存储在堆栈上。并不是说我知道任何这样的实现,但它可以。
回答by Alexei Khlebnikov
Only this statement is true:
只有这个说法是正确的:
vector <Type*> vect; //vect will be on stack and Type* will be on heap.
Type*
pointers are allocated on heap, because amount of the pointers can change dynamically.
Type*
指针在堆上分配,因为指针的数量可以动态变化。
vect
in this case is allocated on stack, because you defined it as a local stack variable.
vect
在这种情况下是在堆栈上分配的,因为您将其定义为本地堆栈变量。
回答by Bingo
vector has an internal allocator
which is in charge of allocating/deallocating memories from heap
for the vector element
. So no matter how you create a vector, its element
is always allocated on the heap
. As for the vector's metadata, it depends on the way you create it.
向量具有内部allocator
,其负责分配/解除分配从存储器中的heap
用于vector element
。所以无论你如何创建一个向量,它element
总是分配在heap
. 至于矢量的元数据,这取决于您创建它的方式。