Pointers & Objects
Objects on Stack Objects created without new are on the stack: 2000 p1 Address Identifier Value 2000 p1 Point 1996 1992 1988 1984 1980 1976 … 1000 999 998 997 996 995
Objects on Heap Objects created with new are on heap Use pointer to keep track of Address Identifier Value 2000 pp 1000 1996 1992 1988 1984 1980 1976 … Point 999 998 997 996 995
Objects on Heap Objects can be on the heap: But member access is tricky
Pointer Member Access Precedence : * comes after . *pp.getX() == *(pp.getX()) Ask memory address to getX??? Address Identifier Value 2000 pp 1000 1996 1992 1988 1984 1980 1976 … Point 999 998 997 996 995
Pointer Member Access Precedence corrected with ( ) Go to point pp points to, ask it to getX() Address Identifier Value 2000 pp 1000 1996 1992 1988 1984 1980 1976 … Point 999 998 997 996 995
Pointer Member Access (*pointer).member Gets ugly
Pointer Member Access (*pointer).member -> : pointer member access Gets ugly -> : pointer member access Dereference pointer, access member in one
Pointer Member Access -> : pointer member access Much better for: Dereference pointer, access member in one Much better for: