0
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...

Christian Rau
  • 45,360
  • 10
  • 108
  • 185
WhatABeautifulWorld
  • 3,198
  • 3
  • 22
  • 30
  • Why do you care? When you try to go messing with alignment, you have to use non-standard compiler-specific extensions. Since you're certainly not sending this out as a packet or writing it to a binary file, just don't worry about it. – Jonathon Reinhart Nov 10 '12 at 18:14
  • Are you on a 64-bit architecture? That's already optimal. A vector takes three words. – Kerrek SB Nov 10 '12 at 18:17
  • I would expect the vector to contain two pointers and one `size_t`, or possibly three pointers. On a 64-bit system, that would be 24 bytes. – Bo Persson Nov 10 '12 at 18:24
  • 1
    When I do sizeof(vector), it gives 16 – WhatABeautifulWorld Nov 10 '12 at 18:29
  • See: http://stackoverflow.com/questions/5232198/about-vectors-growth – Vaibhav Desai Nov 10 '12 at 18:34
  • 1
    @VaibhavDesai The link is irrelevant to this question. OP: What are you trying to do exactly? Doesn't seem right to me to be worrying about the size of a struct for no valid reason? – Khaled Nassar Nov 10 '12 at 18:55

1 Answers1

1

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:

  1. A pointer to the start of the vector.
  2. A pointer to the end of the vector or the number of elements in the vector.
  3. A pointer to the end of the allocated memory or the number of elements allocated.

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 struct would 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 using begin() 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.

Dietmar Kühl
  • 150,225
  • 13
  • 225
  • 380