Memory Impact of Padding

Does padding waste a lot of memory in real programs?

The impact of padding on memory usage can be significant, but it varies depending on your data structures and how they're used. Let's look at some practical examples:

Small Scale Impact

Here's an example where we create an inefficient struct that is 8 bytes larger than it needs to be:

#include <iostream>

struct Inefficient {
  char A;// 1 byte
  int B;// 4 bytes
  char C;// 1 byte
  double D;// 8 bytes
};

struct Optimized {
  double D;// 8 bytes
  int B;// 4 bytes
  char A;// 1 byte
  char C;// 1 byte
};

int main() {
  std::cout << "Inefficient size: "
    << sizeof(Inefficient) << " bytes\n"
    << "Optimized size: "
    << sizeof(Optimized) << " bytes\n";

  // Calculate waste in large array
  constexpr int Count{1000000};
  size_t Waste = (sizeof(Inefficient)
    - sizeof(Optimized)) * Count;

  std::cout << "Memory wasted in array of "
    << Count << " elements: "
    << Waste << " bytes ("
    << (Waste / 1024.0 / 1024.0) << " MB)\n";
}
Inefficient size: 24 bytes
Optimized size: 16 bytes
Memory wasted in array of 1000000 elements: 8000000 bytes (7.62939 MB)

The waste can add up in large collections, but modern systems have strategies to help:

  • Memory pools group similarly-sized allocations
  • Cache lines often need padding anyway
  • Virtual memory can make padding "free" in some cases

Tips to Minimize Waste

Order members by size (largest to smallest):

// Bad layout
struct PlayerBad {
  char Level;// 1 byte + 7 padding
  double Health;// 8 bytes
  char Class;// 1 byte + 7 padding
  double Mana;// 8 bytes
};// Total: 32 bytes

// Better layout
struct PlayerGood {
  double Health;// 8 bytes
  double Mana;// 8 bytes
  char Level;// 1 byte
  char Class;// 1 byte
  // 6 padding bytes
};// Total: 24 bytes

Group small members together:

// Bad - each bool needs 3 padding bytes
struct FlagsBad {
  bool IsActive;
  // 3 padding bytes
  int x;
  
  bool IsVisible;
  // 3 padding bytes
  int y;

  bool IsEnabled;
  // 3 padding bytes
  int z;
};

// Good - bools packed together
struct FlagsGood {
  int x;
  int y;
  int z;
  bool IsActive;
  bool IsVisible;
  bool IsEnabled;
  // 1 padding byte
};

In practice, the memory impact of padding rarely matters for small programs. However, for large-scale applications or memory-constrained systems, careful structure layout can make a significant difference in memory usage.

Padding and Alignment

Learn how memory alignment affects data serialization and how to handle it safely

Questions & Answers

Answers are generated by AI models and may not have been reviewed. Be mindful when running any code on your device.

Why Computer Memory Needs Padding
Why do we need padding at all? Can't the computer just read the bytes it needs?
Consequences of Misaligned Memory
What happens if we try to read memory that isn't properly aligned?
Understanding Natural Alignment
How do I know what the natural alignment of a type should be?
CPU Architecture and Alignment
Do different CPU architectures handle alignment differently?
Alignment with Virtual Functions
How does alignment work with inheritance and virtual functions?
Understanding Union Alignment
How does alignment work with unions?
SIMD and Memory Alignment
How does alignment work with SIMD instructions?
Or Ask your Own Question
Purchase the course to ask your own questions