Memory and Pointers
Articles covering pointers, references, dynamic memory management, and addressing in C++.
Memory and Pointers
The low-level power of C++: addressing, pointers and references; stack vs. heap; and dynamic memory management.
Explore the risks of new/delete, safe patterns, and techniques to prevent memory leaks.
Goal: Apply addressing and dynamic memory correctly to write safe, bug-free C++ code.
TL;DR
- Pointers & references:
int* p,int& r,nullptr. - Dynamic memory:
new/delete,new[]/delete[]. - Arrays & pointer arithmetic:
*(p + i). - Habits/tools: RAII; stepping stone to
std::unique_ptr.
Addresses and Memory Basics
How memory addresses work: bytes, layout, and how pointers reference locations.
References vs Pointers
Key differences: nullability, rebinding, indirection costs, and memory semantics.
Passing Parameters to Functions via Pointers
Call by pointer: modifying values, simulating out params, and performance trade-offs.
Relationship Between Arrays and Pointers
Array-to-pointer decay, contiguous layout, and safe traversal via pointer arithmetic.
Dynamic Memory Management: new and delete
Heap allocations, using new/delete safely, and practices that lead into RAII.
Memory Leaks and Analysis with valgrind
Finding leaks with valgrind, interpreting reports, and common allocation pitfalls.
Multidimensional Arrays and Pointer Usage
2D/3D arrays, row-major layout, and indexing math with pointers in practice.
Introduction to Smart Pointers
Getting started with unique_ptr, shared_ptr, and weak_ptr for safer lifetimes.