In order to understand the memory consumption of std::vector<int>
I wrote:
std::cout << sizeof(std::vector<int>) << std::endl;
This yields 32
. I tried to understand where this value comes from. Some look in the source code revieled that std::vector
stores pointers _MyFirst
, _MyLast
and _MyEnd
which explaines 24 bytes of memory consumption (on my 64 bit system).
What about the last 8
byte? As I understand, the stored allocator does not use any memory. Also this might be implementation defined (is it?), so maybe this helps: I am working with MSVC 2017.5. I do not guarantee to have found all the members by looking into the code; the code looks very obfuscated to me.
Everything seems to be nicely aligned, but may the answer be the following?: Why isn't sizeof for a struct equal to the sum of sizeof of each member?. But I tested it with a simple struct Test { int *a, *b, *c; };
which satisfiessizeof(Test) == 24
.
Some background
In my program, I will have a lot of vectors and it seems that most of them will be empty. This means that the ciritical memory consumption comes from there empty-state, i.e. the heap allocated memory is not so very important.
A simple "just for this usecase"-vector is implemented pretty quickly, so I wondered if I am missing anything and I will need 32 bytes of memory anyway, even with my own implementation (note: I will most probably not implement my own, this is just curiosity).
Update
I tested it again with the following struct:
struct Test
{
int *a, *b, *c;
std::allocator<int> alloc;
};
which now gave sizeof(Test) == 32
. It seems that even though std::allocator
has no memory consuming members (I think), its presence raises Test
's size to 32 byte.
I recognized that sizeof(std::allocator<int>)
yields 1
, but I thought this is how a compiler deals with empty structs and that this is optimized away when it is used as a member. But this seems to be a problem with my compiler.