I'm sorry, I should specify. By word I mean the processor word size (32 bits in this example). The sizes of A, B an C are 8, 16 and 32 bits respectively.
What I'm wondering is, which of the following will be chosen by the compiler:
1. A, B and C are all 8-bit aligned as long as they fit into a single word boundary
(eg. B B _ _, _ B B _ and _ _ B B) are all valid
2. A is 8-bit aligned, B is 16-bit aligned and C is 32-bit aligned
(eg. only B B _ _ and _ _ B B) are valid
3. A, B and C are all 32-bit aligned (an entire 32-bit word is reserved for each of the members)
I'm specifically curious about which option the compiler will choose on an architecture like x86 where unaligned accesses are allowed, but slower.
I don't have good experience dealt with the way memory manages data in this case but AFAIK each type has its own default alignment on memory and a dependent of the architecture or system. For example, alignof(int) is usually 4, that is, int objects will be stored in addresses divisible by 4. However it's possible to change the alignment of a given standard or user-defined data type using alignas, say:
alignas(8) int a{ 22 };
std::cout << &a;
0075FD38
Padding normally happens in classes/structs (easy to be looked up on the Web) and out of them rarely. But nothing is guaranteed I guess.
Compilers, through optimizations (allowed when making no change in the observable behavior of the program's output) can do almost anything and likely pack the elements of a container so that they occupy less number of bytes.
Azadi
Chat Boss
klimi
Один
Ludovic 'Archivist'
mito
Aliyu
\Device\NUL
Yui