vector memory allocation

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.


What I would like to know is, are all of the above statements true?
Sort of. You have correctly asserted when 'vect' is on the heap or on the stack, but from what I know of my compilers implementation of std::vector all of them will be allocated on the heap - the only difference being that for the last example you will have pointers to objects on the heap allocated on the heap. I may be wrong though, or this might be compiler dependent, its normally not a good idea to get too deep into the innards of the standard library.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <vector>

int main()
{
    std::vector<double> vec(1000) ; // vector of 1000 doubles
    
    std::cout << "object 'vec' takes up " << sizeof(vec) << " bytes " 
              << "at address " << std::addressof(vec) 
              << "\nit has an automatic storage duration (typically placed on the runtime-stack)\n" ;
              
    std::cout << "\nthe " << vec.size() << " objects of type double are dynamically allocated\n"
              << "they take up " << vec.size() * sizeof(double) << " bytes of memory "
              << "starting at address " << std::addressof( vec.front() ) 
              << "\nthe standard allocator allocates this memory from the free store\n" ;
}

clang++ -std=c++11 -stdlib=libc++ -O2 -Wall -Wextra -pedantic-errors main.cpp -lsupc++ && ./a.out

object 'vec' takes up 24 bytes at address 0x7fffea8b4520
it has an automatic storage duration (typically placed on the runtime-stack)

the 1000 objects of type double are dynamically allocated
they take up 8000 bytes of memory starting at address 0x24b8010
the standard allocator allocates this memory from the free store

http://coliru.stacked-crooked.com/a/3709a9e37348da90
then vector<Type> *vect is equal to vector<Type*> *vect ?????
Topic archived. No new replies allowed.