🧭 Topic: Pointers
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 (includingvoid*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 withnewanddelete, and highlights common pointer errors such as wild and dangling pointers, variables going out of scope, and the use ofnullptr. Finally, it clarifies the role ofconstwith pointers, compares pointers with reference variables, and explains why pointers are used in functions for efficiency, output parameters, and managing dynamic memory.
📌 Covered in This Topic
- Memory addresses
- What a memory address is and what it represents
- The address-of operator (
&)- How
&is used to obtain a variable’s address
- How
- Pointers
- What a pointer is
- Pointer size (independent of the pointed type)
- Wild pointers
- What they are and why they are dangerous
- Raw pointers vs smart pointers (brief)
- Why raw pointers exist
- A short mention of
unique_ptr,shared_ptr,weak_ptr
- Array names and pointers
- How array names decay to pointers
- Problems when passing arrays to functions (loss of size information)
- Dereferencing (
*)- Accessing the value a pointer points to
- Using dereferencing to access array elements
- Pointer arithmetic
- How incrementing/decrementing pointers works
- Relation to the pointed data type
- Size of pointers
- Why all pointers have the same size on a given architecture
- Pointer data types
- Why pointers must have a data type
- How the type affects dereferencing and arithmetic
- Void pointers (
void*)- What they are
- Limitations and use cases
- Pointer to pointer
- Why and when double pointers are used
- Stack vs heap
- Why the stack is not always sufficient
- When dynamic (heap) allocation is required
- Dynamic memory allocation
newanddeletenew[]anddelete[]
- Reference variables
- What references are
- Differences between references and pointers
- Dangling pointers
- How dangling pointers are created
- How to avoid and handle them
nullptr- Why it exists
- Difference from
NULLand0
- Const and pointers
const int*int* constconst int* const
- Variables going out of scope
- Lifetime of local variables
- Effects on pointers and references
- Pointers and functions
- Passing pointers to functions (input)
- Returning pointers from functions (output)
- Why pointers are used: efficiency, modification, dynamic memory, and shared data
📑 Slides & Materials
🛠️ Workshop & Assignments
🌐 Additional Resources
⏩ Navigation
Tip :
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.