Pointers and
References
kimleo@outlook.com
Concepts
• Pointers
• References (in C++ and other environment)
• Lifetime and Ownerships
• Copy and Move
Terms
1. Pointers is object addresses
2. (C++) Reference is name alias
3. Java uses reference semantics
4. Java object is passed by reference
Pointers
• Pointer (or reference) is an proxy object which
refers to another object indirectly.
Pointers
• Pointer (or reference) is an proxy object which
refers to another object indirectly.
Pointers (in C)
• Object ID (maybe memory address)
• Object Type
Why we need pointer?
Issues with C Pointers
• Dangling Pointers
• Resource Leaks
• Null Pointers
• Weak-typed
References
• (C++)Reference was an simple syntax sugar of
pointers at first.
References
• Reference is a **SAFE**
pointer
• Must be initialized
• Can’t be re-binded
• Can’t be null
Pass By Reference
Reference other than C++
• Java / C#:
• Value & Reference semantics
• Automatic resource management
• Python / JS:
• Everything is reference
• CoW
• Automatic resource management
Copy on Write
Issues with Reference Semantics
• Resource management & Lifetime control
Object Lifetime
• Global / static object
• Local object
• Dynamically created object
• Program data (Constant pool etc.)
Resource Binding Strategy
• Heap object with GC
• Stack object with RAII
• Unique Ownership
Unique Ownership
• If owner of resources is destroyed, ALL RESOURCE it
owned would be released
• Ownership should be unique
Unique Ownership
Move semantics
Move semantics
• Zero-copy
• Resource uniqueness
Borrowing
Reference Model
• Semantics
• Value Semantics
• Reference Semantics
• Move Semantics
• Ownership
• Borrowing

Pointers and References