Interactive Computer Science Tutoring

Learn programming concepts interactively

Undefined Behavior & Memory Safety

Learn to avoid dangerous uninitialized variables

Safety Score: 0/0
What is Undefined Behavior?
Undefined behavior occurs when your program does something the C++ language doesn't define. It's like opening a mystery box - you never know what you'll get!

Dangerous

Uninitialized variables

Safe

Always initialize variables

Memory Contamination Lab
Create variables and see how uninitialized memory can contain garbage values
Bug Detective Game
Analyze code snippets and identify undefined behavior
int main() {
int x;
int y;
std::cout << x + y;
return 0;
}

Two uninitialized integers used in arithmetic

Does this code have undefined behavior?

Generated C++ Code
Live C++ representation with highlighted changes
#include <iostream>
int main() {
// Create variables to see the danger of uninitialized memory
return 0;
}
Memory Safety Rules

Always initialize variables

Use direct initialization: int x42;

Enable compiler warnings

Use -Wall -Wextra to catch uninitialized variables

Initialize arrays and objects

Use {} for zero-initialization