Same mistake. Two languages. Very different outcomes.
Try a simple memory experiment in C++ and Rust to understand why memory safety matters.
Break it yourself
Three buttons, in order: Allocate, Free, then Access the memory you just freed. Watch what the pointer does.
Nothing allocated yet
The heap is where a program asks for memory while it runs. Press Allocate to request a block.
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.
Run both on your own machine
C++ with AddressSanitizer
g++ -g -fsanitize=address -fno-omit-frame-pointer main.cpp -o main./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:4Report 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
cargo new uafcd uafcargo 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
sudocommand, 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.
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.
- Freeing memory does not clear the pointer. In C++ it keeps the old address and becomes a dangling pointer.
- Using it is undefined behavior. It might look fine, print garbage, or crash, and attackers can exploit it.
- Rust tracks one owner per value. After a move or drop, the old name cannot be used, and that is checked at compile time.
- No garbage collector is involved. And no language removes every bug: modern C++ has safer tools, and Rust has limits.
Want more AI tools like this? Follow me
More from me
AI Stickman Videos
One prompt that installs Stickman Video Director and renders your first video.
Read more →Free guideThe Context Kit
Four markdown files that stop your AI coding agent from getting lost.
Read more →Free guideML Model That Can Smell
Set up OpenPOM and predict what any molecule smells like.
Read more →