structure padding on 64bit machine

First of all, structure alignment is not an exact science and may depend on architecture and compiler.

In many case, all structure members are padded according the biggest variable (in byte). On your first structure, all variables are uint32_t, wich is 4 bytes length. Then, your structure size is equal to sizeof(uint32_t) * 5 = 4 * 5 = 20.

On your second structure, the biggest element is uint64_t, wich has a size of 8 bytes. So all elements will be padded according 8 bytes.

The first two uint32_t are padded together, but the third one can't be padded properly : if it was padded with the next integer, the uint64_t would be split in two ! So the compiler decided to let this uint32_t on it 's own to avoid splitting the uint64_t.

Here is an example with your structures and what the address of all variables could be:

struct A
{
  uint32_t var1;   /* ..00 */
  uint32_t var2;   /* ..04 */
  uint32_t var3;   /* ..08 */
  uint32_t var4;   /* ..12 */
  uint32_t var5;   /* ..16 */
};

struct B
{
  uint32_t var1;   /* ..00 */
  uint32_t var2;   /* ..04 */
  uint32_t var3;   /* ..08 */
  uint64_t var5;   /* ..16 */
};

The rule for alignment (on x86 and x86_64) is generally to align a variable on it's size.

In other words, 32-bit variables are aligned on 4 bytes, 64-bit variables on 8 bytes, etc.

In your second case, 4 bytes of padding are added between

uint32_t var3;
uint64_t var5;

to get var5 to align on 8 bytes.

For this reason it is better to order data members from largest to smallest (but it's not that simple due to data locality, readability etc.).