IntermediateTopic 41 of 104Software & Computing
Pointers and references
Variables that refer to memory locations.
In depth
A pointer holds a memory address; a reference is an alias to an object. Pointers enable dynamic structures and efficient parameter passing but risk null dereference, dangling pointers and buffer overflows. Smart pointers (C++) and ownership rules (Rust) manage lifetime.
Key points
- Pointer arithmetic
- Null safety
- Pass by pointer
Exampleint *p=&x; *p=5; changes x via the pointer.
What is Pointers and references?
Variables that refer to memory locations.
Why is it important to understand pointers and references?
Pointers and references let code refer to shared data, which is efficient but a common source of bugs when misused.
What is a dangling pointer?
A pointer to memory that has been freed, and using it leads to undefined behaviour or crashes.
Why do references make copies important?
Two names may refer to the same object, so changing one changes the other, which surprises people who expected a copy.
How is Pointers and references used in practice?
Typical applications include passing a large sensor buffer to a function without copying it, building a linked data structure in memory and accidentally using a freed memory location and causing a crash.
What should I learn before Pointers and references?
Study "Memory management" first (topic 40 of 104 in Software & Computing). It covers the groundwork this topic builds on.
What are the key points of Pointers and references?
Pointer arithmetic. Null safety. Pass by pointer.
Can you give an example of Pointers and references?
int *p=&x; *p=5; changes x via the pointer.
What comes after Pointers and references?
The next topic is "Concurrency and threads", which builds directly on this one.
How difficult is Pointers and references?
It is classed as Intermediate in Software & Computing (topic 41 of 104). It is an intermediate topic. Work through the beginner topics in Software & Computing first.