3

I know the difference for following cases:

case 1: int a[10];

for case 1, memory for array is allocated on stack.

case 2: int *a = new int[10];

for case 2, memory is allocated on heap and a pointer is returned.

But what is the difference between below two declarations, as for vector memory is always allocated on heap

vector<int> v1;
vector<int> *v2 = new vector<int>();
HolyBlackCat
  • 78,603
  • 9
  • 131
  • 207
Ajaychhimpa1
  • 127
  • 7
  • 2
    Possible duplicate of [Difference between static memory allocation and dynamic memory allocation](https://stackoverflow.com/questions/8385322/difference-between-static-memory-allocation-and-dynamic-memory-allocation) –  Jan 24 '19 at 19:22
  • 1
    It's not different from any other type – KABoissonneault Jan 24 '19 at 19:40
  • @KABoissonneault, in array, difference is allocation of memory on stack in one case and on heap in other but in vector memory is always allocated on heap, then what is the difference? – Ajaychhimpa1 Jan 24 '19 at 20:03
  • Regardless of how many elements or type of element a std::vector contains, on ubuntu, g++ v7.3, "sizeof(std::vector)" reports 24 bytes. Thus: v1 occupies 24 bytes of automatic memory, plus n-bytes in dynamic memory to hold the elements. v2 occupies 8 bytes of automatic memory (a pointer), and uses 24 bytes of dynamic memory to hold the vector object, and uses n-bytes in dynamic memory to hold the elements. – 2785528 Jan 24 '19 at 22:52
  • 1
    @Frank not sure this is actually a duplicate since the difference between the two is more than where the memory for the objects come from. There is also the difference in lifetime of the object and the posting you reference is for C where this is C++ and vector can have a specific memory allocator. – Richard Chambers Jan 24 '19 at 23:06

2 Answers2

5

The following two statements create a vector<> however there are several differences between the two.

vector<int> v1;
vector<int> *v2 = new vector<int>();

First of all the actual vector data storage is going to be allocated from the heap or whatever source the designated memory allocator uses (see Where does a std::vector allocate its memory?) and this is the same for both.

The two differences are (1) where the vector<> management data is stored and {2} the lifetime for the vector<> and its allocated memory.

In the first case the vector<> management data is stored on local memory, the stack, and when the vector<> variable goes out of scope, the destructor is called to eliminate the vector data storage space on the heap and the vector management space on the stack. In the first case when the vector<> variable goes out of scope, the vector<> memory is properly released.

In the second case both the vector<> storage data space and the vector<> management space is on the heap.

So when the pointer variable containing the address of the vector<> goes out of scope, the destructor for the vector<> itself is not called. The result is memory that is not recovered since the destructor that would clean up and release the allocated memory for both the data storage area of the vector<> and the management storage area is never called.

One possibility for the second case to ensure that the vector<> is cleaned up properly is to use a smart pointer which when it goes out of scope will also trigger the destructor for the thing pointed to.

In most cases the need for the second case, using new to create the vector<> is rare and the first case is not only the most common but also safer.

Richard Chambers
  • 16,643
  • 4
  • 81
  • 106
4

Only std::vector storage memory is allocated on the heap, the remainder (that is some pointer to the storage + some extra data members) could normally be located on the stack, however by writing new vector<int>() you force the entire thing on to the heap.
Usually there is no need to allocate vectors this way.

paler123
  • 976
  • 6
  • 18