Padding and Alignment

Memory Impact of Padding

Does padding waste a lot of memory in real programs?

Abstract art representing computer programming

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.

This Question is from the Lesson:

Padding and Alignment

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

Answers to questions are automatically generated and may not have been reviewed.

This Question is from the Lesson:

Padding and Alignment

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

sdl2-promo.jpg
Part of the course:

Game Dev with SDL2

Learn C++ and SDL development by creating hands on, practical projects inspired by classic retro games

Free, unlimited access

This course includes:

  • 75 Lessons
  • 100+ Code Samples
  • 91% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Free, Unlimited Access

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Screenshot from Warhammer: Total War
Screenshot from Tomb Raider
Screenshot from Jedi: Fallen Order
Contact|Privacy Policy|Terms of Use
Copyright © 2025 - All Rights Reserved