C++ Pointers & Memory Management Cheat Sheet
Covers C++ pointer syntax, dynamic memory allocation with new and delete, pointer arithmetic, and common memory management pitfalls.
2 PagesIntermediateMar 25, 2026
Pointer Basics
Declaring, dereferencing, and using pointers.
cpp
int x = 42;int* p = &x; // p holds the address of xstd::cout << *p; // dereference: prints 42*p = 100; // modifies x through the pointerint* nullPtr = nullptr; // preferred over NULL/0 in modern C++if (nullPtr == nullptr) { /* safe check before dereferencing */ }int arr[3] = {1, 2, 3};int* arrPtr = arr; // array decays to pointer to first element
Dynamic Memory (new/delete)
Manual heap allocation and deallocation.
cpp
int* p = new int(5); // allocate single int on the heapdelete p; // free itp = nullptr; // avoid dangling pointerint* arr = new int[10]; // allocate arraydelete[] arr; // must use delete[] for arrays, not delete// Mismatched new/delete[] is undefined behavior// Every new must be paired with exactly one delete
Pointer Arithmetic & References
Navigate arrays with pointer math, and compare with references.
cpp
int arr[5] = {10, 20, 30, 40, 50};int* p = arr;std::cout << *(p + 2); // 30, same as arr[2]p++; // advances by sizeof(int) bytesint a = 5;int& ref = a; // reference: alias, must be initialized, can't be null or reboundref = 10; // modifies a directlyvoid increment(int* p) { (*p)++; } // pass by pointervoid increment(int& r) { r++; } // pass by reference (preferred in C++)
Common Pitfalls
Memory bugs that pointers make easy to introduce.
- Dangling Pointer- Pointer that still references memory that has been freed or gone out of scope.
- Memory Leak- Heap memory allocated with new but never delete'd, unreachable and unrecoverable.
- Double Free- Calling delete twice on the same pointer; undefined behavior, often corrupts the heap.
- Wild Pointer- An uninitialized pointer holding a garbage address.
- Buffer Overflow- Writing past the bounds of allocated memory via pointer arithmetic.
- const correctness- const int* p (pointer to const data) vs int* const p (const pointer to mutable data).
Pro Tip
Prefer std::unique_ptr/std::shared_ptr and RAII over raw new/delete in modern C++ - raw pointers should mostly be non-owning observers, with ownership expressed through smart pointers or containers.
Was this cheat sheet helpful?
Explore Topics
#CPointersMemoryManagement#CPointersMemoryManagementCheatSheet#Programming#Intermediate#PointerBasics#Dynamic#Memory#New#CheatSheet#SkillVeris
Advertisement
Sri Hayavadhana Info-Tech
Professional Web Designing Services
- Responsive Websites
- E-commerce Solutions
- SEO Friendly Design
- Fast & Secure
- Support & Maintenance