When vectors are allocated, do they use memory on the heap or the stack?

C++VectorStlHeap MemoryStack Memory

C++ Problem Overview


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?

C++ Solutions


Solution 1 - C++

vector<Type> vect;

will allocate the vector, i.e. the header info, on the stack, but the elements on the free store ("heap").

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).

Solution 2 - C++

> vector 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.

> vector *vect = new vector; //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 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 Types 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.

Solution 3 - C++

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.

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:

  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:

  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:

  int main() {
    vector<Type*> bar;
    Type foo;
    bar.push_back(&foo);
  }

Solution 4 - C++

Only this statement is true:

vector <Type*> vect; //vect will be on stack and Type* will be on heap.

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.

Solution 5 - C++

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPhelodasView Question on Stackoverflow
Solution 1 - C++Fred FooView Answer on Stackoverflow
Solution 2 - C++jpalecekView Answer on Stackoverflow
Solution 3 - C++FlexoView Answer on Stackoverflow
Solution 4 - C++Alexei KhlebnikovView Answer on Stackoverflow
Solution 5 - C++BingoView Answer on Stackoverflow