Understanding computer memory: Bits and bytes

When you create variables in C++, the computer stores that information in Random Access Memory (RAM). Understanding how memory works helps you make better decisions about which data types to use.

The smallest unit of memory is a bit (binary digit), which can hold either 0 or 1. Memory is organized into bytes - collections of 8 bits that the computer treats as a single unit. Each byte has a unique memory address, similar to a street address for a house.

Key concept
When programming in C++, you work with data organized into bytes, not individual bits. Memory addresses point to bytes, not to individual bits.

Here's what a small section of memory might look like:

Address: 0x1000  ->  [01001101]  (8 bits = 1 byte)
Address: 0x1001  ->  [10110010]  (8 bits = 1 byte)
Address: 0x1002  ->  [00111100]  (8 bits = 1 byte)

What are data types?

Since all computer memory is just sequences of bits (0s and 1s), we need a way to interpret what those bits actually mean. A data type tells the compiler how to make sense of the bit patterns stored in memory.

When you declare a variable with a specific type, you're telling the compiler: "Treat the bits at this memory location as this particular kind of data."

For example, when you store the value 97 in an integer variable, the compiler converts 97 into its binary representation and stores those bits in memory. Later, when you use that variable, the compiler reads those same bits and converts them back to 97.

The compiler handles all of this automatically - your job is simply to choose the appropriate data type for what you want to store.

Fundamental data types in C++

C++ provides several built-in data types that you can use immediately without any additional setup. These are called fundamental data types (also known as basic types or primitive types).

Type Category Types Purpose Example Value
Floating Point float, double, long double Numbers with decimal portions 2.71828
Boolean bool True/false values true
Character char, wchar_t, char8_t, char16_t, char32_t Individual text characters 'A'
Integer short, int, long, long long Whole numbers (positive, negative, or zero) 42
Null Pointer std::nullptr_t Represents null pointers nullptr
Void void Absence of type n/a

We'll explore each of these types in depth throughout this chapter.

Understanding "integer" vs "integral"

The terms "integer" and "integral" have specific meanings in C++:

  • Integer: A whole number without a fractional part (-5, 0, 42)
  • Integral: Means "integer-like" and includes standard integer types (short, int, long, long long) plus bool and all character types

All integral types store their values as whole numbers internally. The difference is in how they behave when you output them - standard integer types display as numeric values, while bool and character types have special output behavior.

The three categories of C++ types

C++ organizes its types into three main categories:

  1. Fundamental data types: The basic, built-in types we're studying in this chapter
  2. Compound data types: More complex types that can be combined or customized
  3. Standard library types: Pre-built types from the C++ Standard Library (like std::string)
Note
Strings (sequences of text characters) aren't fundamental types - they're part of the standard library. Despite this, strings are incredibly useful and we'll introduce them early in the next chapter.

The _t naming convention

You'll notice that several types end with _t, such as std::nullptr_t, char8_t, and char16_t. This suffix stands for "type" and is a naming convention used in modern C++. When you see an identifier ending in _t, it's likely a type name.