An instance of a variable can be
- stored directly in memory
- accessed by pointer
- accessed by reference
1. Directly Storage
- The type of a variable has no modifiers.
- The object takes up exactly its size in memory
Cube c;
int i;
uiuc::HSLAPixel p;
2. Storage by Pointer
- The type of a variable is modified with an asterisk (*).
- A pointer takes a “memory address width” of memory (ex: 64 bits on a 64 bit system)
- The pointer “points” to the allocated space of the object.
Cube *c;
int *i;
uiuc::HSLAPixel *p;
3. Storage by Reference
- A reference is an alias to existing memory and is denoted in the type with an ampersand (&).
- A reference does not store memory itself, it is only an alias to another variable.
- The alias must be assigned when the variable is initialized.
- Reference only refer to existing memory. So, its size is zero!
Cube &c = cube; // Alias to the variable `cube`
int &i = count; // Alias to the variable `i`
uiuc::HSLAPixel &p; // Illegal! Must alias something when variable is initialized.