When to Use Small Integer Types

In the real world, when would I choose to use a smaller integer type like int8_t instead of just using regular int?

There are several common scenarios where using smaller integer types makes sense. Here are some practical examples:

Games and Graphics

In games, you often need to store information for lots of objects. For example, if you're making a role-playing game, you might have stats for each character:

#include <iostream>
using namespace std;

int main() {
  // Using regular int for everything (wasteful)
  int Health{100};       // 4 bytes
  int Level{1};          // 4 bytes
  int Strength{15};      // 4 bytes
  int Intelligence{12};  // 4 bytes
  // Total: 16 bytes per character

  // Using appropriate sizes
  // 2 bytes (max 32,767 is plenty)
  int16_t BetterHealth{100};
  // 1 byte (max 127 is enough)
  int8_t BetterLevel{1};      
  // 1 byte (max 127 is enough)
  int8_t BetterStrength{15};
  // 1 byte (max 127 is enough)
  int8_t BetterIntelligence{12};
  // Total: 5 bytes per character

  // If your game has 1000 characters, you save:
  cout << "Memory saved per 1000 characters: "
    << (16 - 5) * 1000 << " bytes";
}
Memory saved per 1000 characters: 11000 bytes

Color Information

In graphics programming, colors are often stored as RGB values from 0-255, making uint8_t perfect:

#include <iostream>
using namespace std;

int main() {
  // Each color component only needs
  // values 0-255
  uint8_t Red{255};  // Perfect fit!
  uint8_t Green{128};
  uint8_t Blue{64};

  // Using regular int would waste 3 bytes
  // per color component!
  // Uses 4 bytes instead of 1
  int WastefulRed{255};
  int WastefulGreen{128};
  int WastefulBlue{64};

  cout << "Memory used for one color"
          " (efficient): "
    << sizeof(Red) + sizeof(Green) + sizeof(Blue)
    << " bytes\n";

  cout << "Memory used for one color"
    " (wasteful): "
   << sizeof(WastefulRed)
    + sizeof(WastefulGreen)
    + sizeof(WastefulBlue)
   << " bytes";
}
Memory used for one color (efficient): 3 bytes
Memory used for one color (wasteful): 12 bytes

Embedded Systems

When programming small devices (like Arduino), memory is very limited. Using smaller types can be the difference between your program fitting in memory or not!

Remember though - don't use smaller types just because you can. Only use them when you:

  • Know the maximum value you need to store
  • Have tested to make sure the smaller type won't cause problems
  • Are working with large amounts of data where the savings matter
  • Are working on a memory-constrained system

Most of the time, using regular int is fine and makes your code simpler and safer.

Types and Literals

Explore how C++ programs store and manage numbers in computer memory, including integer and floating-point types, memory allocation, and overflow handling.

Questions & Answers

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

Memory Usage in Modern Computers
Why do we need to care about memory usage in modern computers when they have so much RAM available?
How Computers Store Text
In the Memory section, you mention bits can be used to represent characters. How can 0s and 1s represent letters?
Monitoring Program Memory Usage
How can I know how much memory my entire program is using? Is there a way to check this?
Uses for Unsigned Integers
If unsigned integers are problematic, why do they exist at all? Are there any real-world cases where they're useful?
Preventing Number Overflow
How can I check if my number will cause an overflow before performing a calculation?
Controlling Decimal Places
When I print floating point numbers, they sometimes show way more decimal places than I want. How can I control this?
Or Ask your Own Question
Get an immediate answer to your specific question using our AI assistant