34

What is the memory overhead of having an empty vector vs having a pointer to a vector?

Option A:

std::vector<int> v;

Option B:

std::vector<int> *v = NULL;

I believe that option B takes 1 32 bit pointer (assuming 32 bit here) How much memory does the empty 'v' take up?

Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212
  • It'd be nice to know what you are trying to get at with this question. Are you looking to make an array of them, and want to know how much space you save by making it an array of pointers? If so, there are probably some other issues you should know about. – T.E.D. Feb 17 '09 at 18:42

6 Answers6

35

As for the question as asked: It depends on the implementation. With MSVC 7.1 this:

std:: cout << sizeof(std::vector<int>) << std::endl;

gives me 16 (bytes). (3 pointers: begin, end, and end of capacity, plus an allocator)

However it should be noted that the pointer-to-vector gives it a larger overhead:

  • in both time and space in the non-empty case
  • in complexity in all cases.
Andrew
  • 5,839
  • 1
  • 51
  • 72
Éric Malenfant
  • 13,938
  • 1
  • 40
  • 42
  • @Legate: beause you have to add the size of the pointer itself to the size of the vector – Éric Malenfant Jan 25 '12 at 21:10
  • @ÉricMalenfant: But if initialized to `NULL`, as in the question's Option B, no vector would be constructed, while with Option A a vector would be default constructed. – Christoph Wurm Jan 26 '12 at 13:41
  • 6
    @Legate: Yes. This is why I wrote "in the non-empty case" – Éric Malenfant Jan 26 '12 at 16:17
  • The ending pointer is not stored, but rather calculated at runtime: `begin() + size()`. – Michael Smith Mar 09 '18 at 20:53
  • @Michael Smith : An implementation can choose to store the size and to calculate the end, or vice versa. The important is that it is implementation dependant. – Éric Malenfant Mar 09 '18 at 21:09
  • @ÉricMalenfant No implementation stores the end. Even if they can theoretically, they don't and would have no valid reason to do so. If they did, they would have to recalculate it every single time the size of the vector changes. – Michael Smith Mar 09 '18 at 22:04
  • @Michael Smith: Well, it seems there is: https://github.com/gcc-mirror/gcc/blob/master/libstdc%2B%2B-v3/include/bits/stl_vector.h . – Éric Malenfant Mar 09 '18 at 22:20
13

It's completely implementation-dependent and you should neither assume nor rely on the details. For what it's worth it's 20-bytes using VC.

sashoalm
  • 75,001
  • 122
  • 434
  • 781
Andrew Grant
  • 58,260
  • 22
  • 130
  • 143
  • On GCC 4.6, it's 12 bytes. I would assume 4 bytes for the pointer, 4 bytes for the size and 4 bytes for the capacity. – Joel Apr 22 '12 at 08:45
5

std::vector v; takes up sizeof(v) space. It might vary by implementation, so run it and find out how much it takes for you.

Welbog
  • 59,154
  • 9
  • 110
  • 123
3

In Visual Studio Community 2017 (Version 15.2), running this code:

#include <iostream>
#include <vector>

using namespace std;

void main()
{
    vector<float> test;
    vector<float>* test2 = &test;
    cout << sizeof(test) << "\n";
    cout << sizeof(test2) << "\n";

    cout << "\n";
    system("pause");
}

Running in 32 bit (x86), I get 16 bytes for the vector and 4 bytes for the vector pointer.

Running in 64 bit (x64), I get 32 bytes for the vector and 8 bytes for the vector pointer.

Andrew
  • 5,839
  • 1
  • 51
  • 72
3

VS2005:

std::vector<int> *ptrToVec = new std::vector<int>();
std::vector<int> vecOfInt;

sizeof(ptrToVec) = 4
sizeof(vecOfInt) = 20

Thanks!

Luc Touraille
  • 79,925
  • 15
  • 92
  • 137
0

Implementation dependant, probably a pointer and two integers for current size and capacity.