Getting the size in bytes of a vector

47,214

Solution 1

Vector stores its elements in an internally-allocated memory array. You can do this:

sizeof(std::vector<int>) + (sizeof(int) * MyVector.size())

This will give you the size of the vector structure itself plus the size of all the ints in it, but it may not include whatever small overhead your memory allocator may impose. I'm not sure there's a platform-independent way to include that.

Solution 2

You probably don't want to know the size of the vector in bytes, because the vector is a non-trivial object that is separate from the content, which is housed in dynamic memory.

std::vector<int> v { 1, 2, 3 };  // v on the stack, v.data() in the heap

What you probably want to know is the size of the data, the number of bytes required to store the current contents of the vector. To do this, you could use

template<typename T>
size_t vectorsizeof(const typename std::vector<T>& vec)
{
    return sizeof(T) * vec.size();
}

or you could just do

size_t bytes = sizeof(vec[0]) * vec.size();

Solution 3

The size of a vector is split into two main parts, the size of the container implementation itself, and the size of all of the elements stored within it.

To get the size of the container implementation you can do what you currently are:

sizeof(std::vector<int>);

To get the size of all the elements stored within it, you can do:

MyVector.size() * sizeof(int)

Then just add them together to get the total size.

Share:
47,214
Davlog
Author by

Davlog

Learning PHP, HTML, C / C++, Python.

Updated on July 22, 2020

Comments

  • Davlog
    Davlog almost 4 years

    Sorry for this maybe simple and stupid question but I couldn't find it anywhere.

    I just don't know how to get the size in bytes of a std::vector.

    std::vector<int>MyVector;   
    /* This will print 24 on my system*/   
    std::cout << "Size of  my vector:\t" << sizeof(MyVector) << std::endl;
    
    for(int i = 0; i < 1000; i++)
       MyVector.push_back(i);
    
    /* This will still print 24...*/    
    std::cout << "Size of  my vector:\t" << sizeof(MyVector) << std::endl;
    

    So how do I get the size of a vector?! Maybe by multiplying 24 (vector size) by the number of items?

    • chris
      chris about 11 years
      Do you mean the total size, in bytes, of all the items? Plus the size of the vector itself?
    • Davlog
      Davlog about 11 years
      I wanna know how much space it takes of my memory. So yeah.
  • joy
    joy about 11 years
    "I wanna know how much space it takes of my memory" -> vector has "capacity()" elements
  • kfsone
    kfsone over 6 years
    sizeof is evaluated at compile time. vec[0] evaluates to the return type of vec.operator []
  • Ben
    Ben about 5 years
    Wouldn't it be better to say sizeof(MyVector) + (sizeof(MyVector[0]) * MyVector.size())` since that would be more robust against MyVector becoming, say, a std::vector<long int, customAllocator<long int>>?
  • Ben
    Ben about 5 years
    As mentioned above, I think sizeof(MyVector[0]) is better than sizeof(int) because that'll stay correct if you change the type of MyVector.
  • xiadeye
    xiadeye over 3 years
    what is sizeof(std::vector<int>) mean?