以下所有陈述都是正确的吗?
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
内部分配内存?
vector<Type> vect;
将在堆栈上分配 vector
,即标头信息,但在空闲存储(“堆”)上分配元素。
vector<Type> *vect = new vector<Type>;
分配免费存储中的所有内容。
vector<Type*> vect;
将在堆栈上分配 vector
并在空闲存储上分配一堆指针,但这些点的位置取决于您如何使用它们(例如,您可以将元素 0 指向空闲存储,将元素 1 指向堆栈)。
向量<类型>向量; //在堆栈上分配vect并且每个类型(使用std :: allocator)也将在堆栈上
不,vect
将在堆栈上,但它在内部用于存储项目的数组将在堆上。这些项目将驻留在该数组中。
矢量<类型> *vect = 新矢量<类型>; //在堆上分配vect,每个类型将在堆栈上分配
不。与上面相同,除了 vector
类也将在堆上。
向量<类型*>向量; //vect 将在堆栈上,而 Type* 将在堆上。
vect
将在堆栈上,它的项目(指向 Type
的指针)将在堆上,您无法确定指针指向的 Type
将在哪里。可能在堆栈上,可能在堆上,可能在全局数据中,可能无处(即 NULL
指针)。
顺便说一句,该实现实际上可以将一些向量(通常是小尺寸的)完全存储在堆栈上。不是说我知道任何这样的实现,但它可以。
假设一个实现实际上有一个堆栈和一个堆(标准 C++ 不要求有这样的东西),唯一正确的陈述是最后一个。
vector<Type> vect;
//allocates vect on stack and each of the Type (using std::allocator) also will be on the stack
这是真的,除了最后一部分(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);
}
同样地:
vector<Type> *vect = new vector<Type>; //allocates vect on heap and each of the Type will be allocated on stack
除了最后一部分之外是正确的,有一个类似的反例:
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);
}
为了:
vector<Type*> vect; //vect will be on stack and Type* will be on heap.
这是真的,但请注意,Type*
指针将位于堆上,但它们指向的 Type
实例不必是:
int main() {
vector<Type*> bar;
Type foo;
bar.push_back(&foo);
}
delete
)。
只有这句话是正确的:
vector <Type*> vect; //vect will be on stack and Type* will be on heap.
Type*
指针存储在堆上,因为指针的数量可以动态变化。
在这种情况下,vect
分配在堆栈上,因为您将其定义为本地堆栈变量。
vector 有一个内部 allocator
,它负责从 heap
为 vector element
分配/取消分配内存。因此,无论您如何创建向量,它的 element
总是分配在 heap
上。至于向量的元数据,这取决于您创建它的方式。
不定期副业成功案例分享
vector<Type> vect;
,由于元素在堆上,头信息在栈上,当头信息从内存中删除时,如函数返回,元素内存会发生什么?它们是否与标题信息一起回收?如果不是,这会导致内存泄漏吗?