Back to main
Interactive explainer · C++ vs Rust

Same mistake. Two languages. Very different outcomes.

Try a simple memory experiment in C++ and Rust to understand why memory safety matters.

Start the experimentTakes about 3 minutes

Break it yourself

Three buttons, in order: Allocate, Free, then Access the memory you just freed. Watch what the pointer does.

Interactive simulation
STACKHEAPptrnot setin use0x5a380x5a3cfree0x5a400x5a44in use0x5a480x5a4c

Nothing allocated yet

The heap is where a program asks for memory while it runs. Press Allocate to request a block.

Addresses and values are illustrative.

The same bug, as code

Both programs do what you just did: allocate, free, then use. Tap a step to see the matching line in each language, then run them.

main.cpp
#include <iostream>
int main() {
int* ptr = new int(42);
delete ptr;
std::cout << *ptr;
}
main.rs
fn main() {
let ptr = Box::new(42);
drop(ptr);
println!("{}", *ptr);
}
Run both on your own machine

C++ with AddressSanitizer

  1. g++ -g -fsanitize=address -fno-omit-frame-pointer main.cpp -o main
  2. ./main

Save the code as main.cpp first. clang++ takes the same flags. A plain build may print anything, but AddressSanitizer tracks every allocation and stops at the bad read with a report like this (from GCC 13.2):

ERROR: AddressSanitizer: heap-use-after-free
READ of size 4 at 0x602000000010 thread T0
    #0 in main main.cpp:8
freed by thread T0 here:
    #1 in main main.cpp:6
previously allocated by thread T0 here:
    #1 in main main.cpp:4

Report shortened; yours will have full addresses and stack frames. On Windows, use WSL, or MSVC with cl /fsanitize=address /Zi main.cpp.

Rust with Cargo

  1. cargo new uaf
  2. cd uaf
  3. cargo run

Install Rust from rustup.rs. After cd uaf, replace src/main.rs with the Rust code above, then run. You should get error E0382 and no binary. Or paste it into the Rust Playground with nothing to install.

Why this matters in the real world

  • C++ runs the software you use all day

    Browsers, game engines and operating systems are written largely in C and C++ because the language gives direct control over memory and speed, with decades of existing code behind it. That same direct control is what lets a dangling pointer exist.

  • A bad read is more than a wrong number

    Memory safety bugs can crash a program, leak data that was sitting in reused memory, or let an attacker who controls that memory run their own code. You saw the start of it when the stale pointer read 1337.

  • ~70%

    Microsoft

    In 2019, Microsoft's Security Response Center reported that roughly 70% of the vulnerabilities it assigns a CVE each year are memory safety issues.
    Source: MSRC, “A proactive approach to more secure code” (July 2019)

  • ~70%

    Chromium

    The Chromium project found that around 70% of its high-severity security bugs were memory unsafety problems, from an analysis of 912 high or critical bugs in the stable channel since 2015.
    Source: The Chromium Projects, “Memory safety”

  • sudo-rs

    Ubuntu 25.10

    Ubuntu announced in May 2025 that sudo-rs, a Rust rewrite of the sudo command, becomes the default implementation starting with Ubuntu 25.10. The original stays available to install.
    Source: Ubuntu Discourse, “Adopting sudo-rs By Default in Ubuntu 25.10”

Both 70% figures are historical studies of those two codebases, not a measurement of all software.

How Rust solves this

Four ideas, each with something to press. Together they give safe Rust memory safety without a garbage collector: the compiler works out where memory is freed, so nothing has to scan for it while the program runs.

Every value has exactly one owner. Assigning or passing it somewhere moves ownership, and the old name stops working. That single rule is what made drop(ptr) kill the pointer in the experiment.

aowner
String "hi"
bempty

a owns the string. Move it, then try to use a.

Modern C++ is safer than this example

Raw new and delete are discouraged today. With RAII and smart pointers such as std::make_unique<int>(42), memory is released automatically when its owner goes out of scope. The difference is enforcement: these are good habits you choose to follow, and the compiler still accepts code that breaks them.

Rust is not immune to bugs

The guarantees cover safe Rust. Code inside an unsafe block can still get memory wrong, and safe Rust can still have logic errors, panics, deadlocks and even memory leaks. Rust removes one large class of bugs, not all of them. Read more in the Rust Book on ownership and the E0382 reference.

Key takeaway

C++ gives you control over memory.Rust gives you low-level control with additional safety rules enforced by the compiler.
  1. Freeing memory does not clear the pointer. In C++ it keeps the old address and becomes a dangling pointer.
  2. Using it is undefined behavior. It might look fine, print garbage, or crash, and attackers can exploit it.
  3. Rust tracks one owner per value. After a move or drop, the old name cannot be used, and that is checked at compile time.
  4. No garbage collector is involved. And no language removes every bug: modern C++ has safer tools, and Rust has limits.
cursor