I'm using Linux on a 64-bit machine and I use g++4.8.
The sizeof
operator applied on a std::vector
returns :
sizeof(std::vector<float>) = 24
sizeof(std::vector<double>) = 24
sizeof(std::vector<long double>) = 24
My question is : is there any way to create a vector with a smaller size based on the fact that my application will never need vectors of more than 1 GB
of elements ? Because optimally, I could have a vector of 16 bytes : the pointer to the beginning (8 bytes), the current size (4 bytes) and the current capacity (4 bytes). Do I have to recode my own vector from scratch or can I reduce the size with a custom allocator ?
This size is very critical for me as I work on supercomputers with more than 100 TB of memory, and every byte I can gain on my basic classes could finally save several TB of memory.
No, you can't.
The original idea with allocators was that they could define objects for pointers and reference types to the data. However, that's not really allowed anymore; allocator<T>::pointer
must be T*
, and containers are freely allowed to assume that it is.
So if you truly need a vector to use some kind of compact pointer representation or something, you're going to have to implement one yourself.
The standard library containers are good defaults for most uses. But for those with specialized needs, you may have to replace them with something else. That would appear to be the case here.