← All posts
November 23, 2026 · 3 min read

Struct memory layout in C++: the real bytes, offsets and padding

Struct memory layout in C++, byte by byte: char, short, int in 8 bytes, one padding byte, and a real dump read low byte first: 41 00 02 01 e8 03 00 00.

Struct memory layout in C++ is simple to state: a struct's bytes are its members' bytes, in declaration order, with padding bytes wherever a member's alignment needs a gap. The best way to see it is to print the real bytes. struct S { char c; short s; int i; }; has 1 + 2 + 4 = 7 bytes of members, and sizeof(S) is 8.

This is the written version of Understanding C++ #11. Every number below is printed by one program, compiled with g++ -std=c++20 -Wall -O2 (GCC 13) on x86-64 Linux, where short is 2 bytes with an alignment of 2, int is 4 bytes with an alignment of 4, and memory is little-endian.

Eight empty byte cells at offsets 0 to 7: 1 + 2 + 4 = 7 bytes of fields, sizeof(S) = 8

Where does each member go?

The program first zeroes all eight bytes with std::memset, then stores x.c = 'A', x.s = 258 and x.i = 1000. offsetof(S, member) gives each member's distance in bytes from the start of the struct.

struct S { char c; short s; int i; }: c at offset 0, s at 2 and 3, i at 4 to 7, all eight bytes 00 after memset

c is a char, so it takes offset 0. The next free offset is 1, but a short must start at an even offset (alignof(short) is 2), so the compiler leaves byte 1 as padding and offsetof(S, s) prints 2. s takes offsets 2 and 3, and i starts at 4, a multiple of 4, with no gap needed. That fills exactly 8 bytes, which is already a multiple of the struct's alignment (4), so there is no padding at the end.

alignof(short) = 2 and offsetof(S, s) = 2: byte 1 is padding, c holds 41

offset 0 1 2 3 4 5 6 7
member c pad s s i i i i
byte 41 00 02 01 e8 03 00 00

How do you read the bytes?

The last loop walks the struct through an unsigned char* and prints each byte in hex, lowest address first: 41 00 02 01 e8 03 00 00.

  • Offset 0 is 41: the character 'A' is 0x41 (65).
  • Offset 1 is 00: the padding byte. It reads 0 here only because memset zeroed it and nothing wrote over it. The language does not promise any particular value in padding after you store into members, so never rely on it.
  • Offsets 2 and 3 are 02 01: 258 is 0x0102. x86-64 is little-endian, so the low byte, 02, comes first, at the lower address.
  • Offsets 4 to 7 are e8 03 00 00: 1000 is 0x000003e8, again low byte first, with two zero bytes on top.

258 = 0x0102 stored low byte first: 02 at offset 2, 01 at offset 3

What is the address of a member?

A member's address is the struct's address plus its offset. The second line of output shows &x and &x.i for one run. The addresses change every run (the stack is placed at a random address), but &x.i is always &x plus 4. In this run x sat at 0x7ffdc47d7b60 and i at 0x7ffdc47d7b64.

The full dump 41 00 02 01 e8 03 00 00 under the addresses of one run: &x ends in 60, &x.i ends in 64

So a struct is not a container with a separate structure of its own. It is a run of bytes: each member at a fixed offset known at compile time, in the order you declared them, with gaps for alignment. Accessing x.i is reading 4 bytes at &x + 4.

7 bytes of fields plus 1 padding byte, sizeof(S) = 8, with the whole program layout.cpp below

The program and its output

#include <cstddef>
#include <cstdio>
#include <cstring>
struct S { char c; short s; int i; };
int main() {
  S x; std::memset(&x, 0, sizeof x);
  x.c = 'A'; x.s = 258; x.i = 1000;
  std::printf("%zu %zu %zu\n", offsetof(S, s),
              offsetof(S, i), sizeof x);
  std::printf("%p %p\n", (void*)&x, (void*)&x.i);
  auto* b = (unsigned char*)&x;
  for (std::size_t k = 0; k < sizeof x; k++)
    std::printf("%02x ", b[k]);
}
2 4 8
0x7ffdc47d7b60 0x7ffdc47d7b64
41 00 02 01 e8 03 00 00 

Line by line: 2 4 8 are offsetof(S, s), offsetof(S, i) and sizeof(S); the two addresses are &x and &x.i (yours will differ, 4 apart); the last line is the eight bytes of x.

Try it

Keep everything the same but store x.i = 258 instead of 1000. What byte is at offset 4 now, and what are all eight bytes? Work it out from the little-endian rule, then run the program to check.