एक कार्यान्वयन को मानते हुए जो वास्तव में एक स्टैक और एक ढेर है (मानक सी ++ ऐसी चीजों के लिए कोई आवश्यकता नहीं बनाता है) एकमात्र सही कथन अंतिम है।
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);
}