分配向量时,它们使用堆上的内存还是堆栈上的内存?
以下所有陈述都正确吗?
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
分配内存?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
将在堆栈上分配向量(即标头信息),但在空闲存储(“堆”)上分配元素。
分配空闲存储上的所有内容(除了位于堆栈上的
vect
指针)。将在堆栈上分配向量并在自由存储上分配一堆指针,但这些点的位置取决于您如何使用它们(您可以将元素 0 指向自由存储,将元素 1 指向堆栈,说)。
will allocate the
vector
, i.e. the header info, on the stack, but the elements on the free store ("heap").allocates everything on the free store (except
vect
pointer, which is on the stack).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).不,
vect
将位于堆栈上,但它内部用于存储项目的数组将位于堆上。这些项目将驻留在该数组中。不会。与上面相同,只是
vector
类也将位于堆上。vect
将位于堆栈上,其项(指向Type
的指针)将位于堆上,并且您无法判断Type
将位于何处code> 是指针所指向的。可能在堆栈上,可能在堆上,可能在全局数据中,也可能不在任何地方(即NULL
指针)。顺便说一句,该实现实际上可以将一些向量(通常尺寸较小)完全存储在堆栈上。我不知道有任何这样的实现,但它可以。
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.No. Same as above, except the
vector
class will be on the heap as well.vect
will be on the stack, its items (pointers toType
) will be on the heap, and you can't tell where will be theType
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).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.
假设一个实现实际上有一个栈和一个堆(标准 C++ 不要求有这样的东西),唯一正确的语句是最后一个。
这是真的,除了最后一部分(
Type
不会在堆栈上)。想象一下:同样:
除了最后一部分之外,
为真,有一个类似的反例:对于:
这是真的,但请注意,
Type*
指针将位于堆上,但Type< /code> 它们指向的实例不必是:
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.
This is true, except for the last part (
Type
won't be on the stack). Imagine:Likewise:
True except the last part, with a similar counter example:
For:
this is true, but note here that the
Type*
pointers will be on the heap, but theType
instances they point to need not be:矢量有一个内部分配器,负责从
堆
中为矢量元素
分配/释放内存。因此,无论您如何创建向量,其元素
始终分配在堆
上。至于矢量的元数据,这取决于您创建它的方式。vector has an internal
allocator
which is in charge of allocating/deallocating memories fromheap
for thevector element
. So no matter how you create a vector, itselement
is always allocated on theheap
. As for the vector's metadata, it depends on the way you create it.只有这句话是正确的:
Type*
指针存储在堆上,因为指针的数量可以动态变化。在这种情况下,
vect
是在堆栈上分配的,因为您将其定义为本地堆栈变量。Only this statement is true:
Type*
pointers are stored on heap, because amount of the pointers can change dynamically.vect
in this case is allocated on stack, because you defined it as a local stack variable.