|
| 1 | +# 🧭 Topic: Pointers |
| 2 | + |
| 3 | +> [!info] **Quick Overview :** This topic covers how memory is accessed and managed in C++, starting from memory addresses and the address-of operator (`&`), and moving to pointers as variables that store addresses. It explains dereferencing, pointer arithmetic, pointer types (including `void*` and pointer-to-pointer), and why all pointers have the same size on a given architecture. The relationship between arrays and pointers is discussed, including common pitfalls when passing arrays to functions. The overview also contrasts stack and heap memory, introduces dynamic allocation with `new` and `delete`, and highlights common pointer errors such as wild and dangling pointers, variables going out of scope, and the use of `nullptr`. Finally, it clarifies the role of `const` with pointers, compares pointers with reference variables, and explains why pointers are used in functions for efficiency, output parameters, and managing dynamic memory. |
| 4 | +
|
| 5 | + |
| 6 | +## 📌 Covered in This Topic |
| 7 | +- **Memory addresses** |
| 8 | + - What a memory address is and what it represents |
| 9 | +- **The address-of operator (`&`)** |
| 10 | + - How `&` is used to obtain a variable’s address |
| 11 | +- **Pointers** |
| 12 | + - What a pointer is |
| 13 | + - Pointer size (independent of the pointed type) |
| 14 | +- **Wild pointers** |
| 15 | + - What they are and why they are dangerous |
| 16 | +- **Raw pointers vs smart pointers (brief)** |
| 17 | + - Why raw pointers exist |
| 18 | + - A short mention of `unique_ptr`, `shared_ptr`, `weak_ptr` |
| 19 | +- **Array names and pointers** |
| 20 | + - How array names decay to pointers |
| 21 | + - Problems when passing arrays to functions (loss of size information) |
| 22 | +- **Dereferencing (`*`)** |
| 23 | + - Accessing the value a pointer points to |
| 24 | + - Using dereferencing to access array elements |
| 25 | +- **Pointer arithmetic** |
| 26 | + - How incrementing/decrementing pointers works |
| 27 | + - Relation to the pointed data type |
| 28 | +- **Size of pointers** |
| 29 | + - Why all pointers have the same size on a given architecture |
| 30 | +- **Pointer data types** |
| 31 | + - Why pointers must have a data type |
| 32 | + - How the type affects dereferencing and arithmetic |
| 33 | +- **Void pointers (`void*`)** |
| 34 | + - What they are |
| 35 | + - Limitations and use cases |
| 36 | +- **Pointer to pointer** |
| 37 | + - Why and when double pointers are used |
| 38 | +- **Stack vs heap** |
| 39 | + - Why the stack is not always sufficient |
| 40 | + - When dynamic (heap) allocation is required |
| 41 | +- **Dynamic memory allocation** |
| 42 | + - `new` and `delete` |
| 43 | + - `new[]` and `delete[]` |
| 44 | +- **Reference variables** |
| 45 | + - What references are |
| 46 | + - Differences between references and pointers |
| 47 | +- **Dangling pointers** |
| 48 | + - How dangling pointers are created |
| 49 | + - How to avoid and handle them |
| 50 | +- **`nullptr`** |
| 51 | + - Why it exists |
| 52 | + - Difference from `NULL` and `0` |
| 53 | +- **Const and pointers** |
| 54 | + - `const int*` |
| 55 | + - `int* const` |
| 56 | + - `const int* const` |
| 57 | +- **Variables going out of scope** |
| 58 | + - Lifetime of local variables |
| 59 | + - Effects on pointers and references |
| 60 | +- **Pointers and functions** |
| 61 | + - Passing pointers to functions (input) |
| 62 | + - Returning pointers from functions (output) |
| 63 | + - Why pointers are used: efficiency, modification, dynamic memory, and shared data |
| 64 | + |
| 65 | +--- |
| 66 | +## 📑 Slides & Materials |
| 67 | +- 👨🏫 [Professor Slides (PDF)](01-topics/07-pointers/4-ProfessorSlides.md) |
| 68 | +- 🧑🏫 [TA Workshop Slides (PDF)](01-topics/07-pointers/5-TASlides.md) |
| 69 | + |
| 70 | +--- |
| 71 | +## 🛠️ Workshop & Assignments |
| 72 | + |
| 73 | +- 🧮 [Assignments](2-Assignment.md) |
| 74 | +- ❓ [Q&A and Common Issues]() |
| 75 | + |
| 76 | +--- |
| 77 | +## 🌐 Additional Resources |
| 78 | + |
| 79 | + |
| 80 | +--- |
| 81 | +## ⏩ Navigation |
| 82 | + |
| 83 | +- ⬅️ [Previous : Functions 2 - Recursion](../06-functions-2/0-Overview.md) |
| 84 | +- ➡️ [Next Topic: File & Struct](../08-file-struct/0-Overview.md) |
| 85 | + |
| 86 | + |
| 87 | +--- |
| 88 | + |
| 89 | +> [!tip] **Tip :** |
| 90 | +> |
| 91 | +> If something doesn’t make sense — **don’t stay stuck alone**. Ask the TA to clarify it right away. Often, a quick question can save you a lot of time and frustration. Remember: if you’re confused, chances are others are too. |
| 92 | +
|
0 commit comments