Memory Management Programs
C++ memory programs: pointers, pointer arithmetic, double pointers, function pointers, nullptr, new and delete, dynamic arrays and objects, leak prevention, and smart pointers.
11 programs · open one, then the next
Start with Basic Pointers- 01Basic PointersBeginnerLearn C++ pointers from scratch: store an address with &, read and change a value through a pointer with *, and see why ptr and &ptr print different addresses.Open example →cpp#include <iostream> using namespace std; int main() {
- 02Pointer ArithmeticBeginnerMove through an array with pointer arithmetic in C++: ptr + i, ptr++, and ptr--. Learn why addresses jump by sizeof(int) and which positions are legal.Open example →cpp#include <iostream> using namespace std; int main() {
- 03Pointer to PointerIntermediateUnderstand double pointers in C++: an int** that stores the address of an int*. Follow *ptrToPtr and **ptrToPtr step by step and see where they are used.Open example →cpp#include <iostream> using namespace std; int main() {
- 04Array of PointersIntermediateStore addresses in an array of pointers in C++, change variables through them, and keep C-strings in const char* names[]. Also: int* p[5] vs int (*p)[5].Open example →cpp#include <iostream> using namespace std; int main() {
- 05Pointer to FunctionIntermediateStore and call functions through pointers in C++: arrays of function pointers, typedef, callbacks, returning a function pointer, and passing one to std::sort.Open example →cpp#include <iostream> #include <vector> #include <algorithm> #include <string>
- 06Null PointerBeginnerUse nullptr in C++ to mark a pointer that points nowhere, check it before dereferencing, and reset it after delete. Covers NULL vs nullptr and bad_alloc.Open example →cpp#include <iostream> using namespace std; int main() {
- 07new and delete OperatorsBeginnerAllocate heap memory in C++ with new and new[], then free it with matching delete and delete[]. Covers uninitialized arrays, leaks, and safer options.Open example →cpp#include <iostream> using namespace std; int main() {
- 08Dynamic Array AllocationBeginnerCreate an array sized at run time with new int[size] in C++, then find its sum, average, max, and min. Covers delete[], size 0, and why vector is safer.Open example →cpp#include <iostream> using namespace std; int main() {
- 09Dynamic Object CreationIntermediateCreate objects on the heap with new in C++, use them through pointers with ->, and delete them so the destructor frees the marks array each one owns.Open example →cpp#include <iostream> #include <string> using namespace std;
- 10Memory Leak PreventionIntermediatePrevent memory leaks in C++ with a Resource class that frees memory in its destructor and follows the Rule of Three with a deep copy and safe assignment.Open example →cpp#include <iostream> using namespace std; class Resource {
- 11Smart PointersIntermediateManage memory automatically in C++ with unique_ptr and shared_ptr. See ownership move, reference counting with use_count, and when each object dies.Open example →cpp#include <iostream> #include <memory> using namespace std;