Course contentsShow
← C
Lesson 839 of 1,21911. The Preprocessor and BuildFree lesson

Examining raw memory with `x`

Examining raw memory with x

What you'll learn: How to peer directly into your computer's RAM to see how variables and structures are actually stored at the byte level.

Looking under the hood

Until now, you have likely used the print command in GDB to check variable values. While print is great, it shows you what the compiler wants you to see—formatted integers or pretty-printed strings. Sometimes, especially when debugging low-level pointer issues or memory corruption, you need to see the "raw" reality.

The x command (short for eXamine) allows you to look at a specific memory address and see exactly what bits are sitting there, regardless of what the variable type claims to be.

The syntax of x

The command follows a specific pattern: x/nfu <address>.

  • n: How many units to display.
  • f: The format (e.g., x for hex, d for decimal, c for char).
  • u: The unit size (b for byte, h for halfword/2 bytes, w for word/4 bytes).

Think of your memory like a massive warehouse of tiny lockers. The print command asks the manager what’s inside a locker; the x command lets you walk up to the locker and stare through the mesh wire yourself.

A concrete example

Suppose you have an array of integers. On most systems, an int is 4 bytes. If we look at it byte-by-byte, we can see how the data is laid out.

#include <stdio.h>

int main() {
    int secret_codes[] = {10, 20, 30};
    // Imagine we are stopped here in GDB
    return 0;
}

If you are at a breakpoint in GDB, you can run: x/12xb &secret_codes

This tells GDB: "Examine (x) 12 units, formatted as hex (x), one byte (b) at a time, starting at the address of secret_codes."

You might see output like 0x0a 0x00 0x00 0x00. That 0x0a is the number 10. The three 0x00 bytes that follow are the rest of that 4-byte integer. Seeing this raw view is vital for spotting "buffer overflows" where data from one variable accidentally spills over into the memory "lockers" belonging to another.

Key takeaway

Use x to bypass variable types and inspect the raw, physical bytes stored at any memory address.