假设实现确实具有堆栈和堆(标准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) {
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) {
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);
}