How many bytes does a vector use?
Vectors use memory in two places: 1. within the vector object itself, and 2. on the heap, where the vector elements are stored.
Compiler implementers have a lot of freedom in how they allocate this memory. Typically, however, the vector object itself contains the parts that to not change in size. Here is what I found in Microsoft's implementation:
A pointer to the beginning of data on the heap, i.e., to the first data element.
A pointer to the end of data, i.e., one element beyond the final data element. Using this pointer, capacity()
is calculated as end - first
.
A pointer marking the last element in use, i.e., a pointer to the element following the last data element in use. With this pointer, size()
is calculated as last - first
.
The array of data elements is stored on the heap, potentially with other implementation specific information (or not).
For a given vector v
, assuming that nothing except the array of data elements is stored on the heap, the number of bytes used will be:
sizeof(v) + (v.data() == nullptr ? 0 : v.capacity() * sizeof(*v.data()))
The vector in the original question is a vector of pointers, specifically, pointers to const char
. The vector is initialized by an initializer_list
with 6 string literals
. The string literals are stored in static memory, while the pointers to them are stored on the heap, as vector data. Presumably, the capacity was allocated at 6, but I believe that detail is implementation specific. The size of each element is the size of a data pointer, usually that is the sizeof(std::size_t)
.
Here is a short program to calculate the total number of bytes used. Hopefully, the original poster will run it on his system, and tell us what he gets.
#include <cstddef>
#include <iostream>
#include <vector>
int main()
{
std::vector<const char*> v = { "Microsoft", "Apple", "DELL", "Accer", "Lenovo", "hp" };
std::cout << std::boolalpha
<< "sizeof(v) : " << sizeof(v)
<< "\nv.size() : " << v.size()
<< "\nv.capactity() : " << v.capacity()
<< "\nv.data() == nullptr : " << (v.data() == nullptr)
<< "\nv.data() : " << v.data()
<< "\nsizeof(*v.data()) : " << sizeof(*v.data())
<< "\nsizeof(std::size_t) : " << sizeof(std::size_t)
<< "\nTotal bytes used : " << sizeof(v) + (v.data() == nullptr ? 0 : v.capacity() * sizeof(*v.data()))
<< "\n\n";
return 0;
}
I ran on MSVC, targeting x64, so my pointers were 8 bytes each. Evidently, there is a fourth item stored in the vector object, because the sizeof(v)
came out at 32, rather than 24.
sizeof(v) : 32
v.size() : 6
v.capactity() : 6
v.data() == nullptr : false
v.data() : 000002952A005030
sizeof(*v.data()) : 8
sizeof(std::size_t) : 8
Total bytes used : 80
In reading through Microsoft's source code for header <vector>
, I was able to locate the three pointers mentioned above. They are stored in an object named _Mypair
as _Mypair._Myval2
. I could not, however, find the definition for _Mypair._Myval1
, which, presumably, is the fourth item.
When I recompiled, targeting x86, the size was cut in half. That makes sense, because std::size_t
, the size used for pointers, had its size cut down to 4 bytes.
sizeof(v) : 16
v.size() : 6
v.capactity() : 6
v.data() == nullptr : false
v.data() : 00AEC640
sizeof(*v.data()) : 4
sizeof(std::size_t) : 4
Total bytes used : 40