19.3 Comparing C and C++ Approaches
C:
- Raw pointers are integral and must be managed manually.
- Frequent
malloc()
andfree()
calls. - Common errors include double frees, memory leaks, and dangling pointers.
C++ Smart Pointers:
std::unique_ptr
andstd::shared_ptr
automate cleanup, reducing manualnew
/delete
.- They rely on runtime checks or reference counting.
- Cycles or subtle issues can still occur if not carefully managed.