struct bar {
int32 a; // 4 bytes
int32 b; // 4 bytes
vector<int> c; // something like 8 or 16 bytes?
}
When I do sizeof(bar) it gives 32 bytes!
Any way I could align it to be something smaller? Any potential
penalty for that? I am not quite sure about the vector part…
The normal implementation of
std::vector<T, A>is to have a pointer to have three pointers plus, for stateful allocators, whatever it takes to maintain the allocator:When putting this into a type, it will likely be padded to 4 words to improve access times. When you combine a vector like this with two more data members, you’ll naturally get a type which occupies 8 words, three of which are to align the type for fast access.
If you really care about the size of your
structs containing vector because the vector is normally empty and only rarely contains data, you’d probably prefer an implementation with a different layout: The vector would still need to start the information mentioned above but it could start the data with the allocated buffer: When allocating memory, the vector would allocate extra space to prepend its administration information to the actual values and store a pointer to the allocated data (probably to the first element which, however, makes recovery of the administration information not portable).Using a representation just described, the size of the vector would be one word and the total size of your
structwould be three words plus padding, i.e., probably 4 words. The better size is traded for a small overhead when the size or the capacity of the vector is needed (e.g., when usingbegin()and `end()).In general, the size of the vectors tends not to matter because the actual memory consumption of the vectors is down to the elements in the vector.