Quick Answer
Low-level memory access with pointer operations.
Understanding the Issue
Pointers enable direct memory manipulation but require careful handling to avoid segmentation faults and memory leaks.
The Problem
This code demonstrates the issue:
Cpp
Error
// Problem: Dangling pointer
int* create() {
int x = 10;
return &x; // Returns address of local
}
The Solution
Here's the corrected code:
Cpp
Fixed
// Solution 1: Dynamic allocation
int* safe_create() {
return new int(10); // Caller must delete
}
// Solution 2: Smart pointer
unique_ptr<int> best_create() {
return make_unique<int>(10);
}
Key Takeaways
Understand pointer lifetimes and prefer smart pointers.