1
Pointers and Expressions
Prof. Evan Chang
Meeting 16, CSCI 3155, Fall 2009
Announcements
• HW5 due Thu Oct 22 at 11:55pm
• How was Project 1?
• How are recitations?
2
Finish Unions
Unions
• What are they for?
• Kinds of unions?
4
Unions
5
Exercise
• Define types and code for evaluating an expression language in C (and in SML):
e ::= n | -e | e + e | e * e
6
2
Pointers
Pointers
• Dangling pointer bug
• Memory leak bug
8
Pointers
• A pointer type include values that range over memory addresses
• ... and often an additional distinguished value null.
– “The Billion Dollar Mistake”
9
Pointers
• A pointer type include values that range over memory addresses
• ... and often an additional distinguished value null.– SML analog?
10
Pointer Operations
• What are they?
11
Problem: Dangling Pointer
• What is it?
12
3
Problem: Dangling Pointer
• What is it?
13
Problem: Dangling Pointer
• What problems can it cause?
14
Problem: Dangling Pointer
• What problems can it cause?
15
Problem: Dangling Pointer
• Possible solutions?
16
Problem: Memory Leak
• What is it?
17
Problem: Memory Leak
• What is it?
18
4
Problem: Memory Leak
• What problems can it cause?
19
Problem: Memory Leak
• What problems can it cause?
20
Problem: Memory Leak
• Possible solutions?
21
Garbage Collection
22
Garbage Collection: Reference Counting
23
Garbage Collection: Reference Counting
24
5
Garbage Collection: Reference Counting
25
Garbage Collection: Mark-and-Sweep
26
For Next Time
• Reading
• Online discussion forum– ≥1 substantive question, comment, or answer each week
• Work on HW3
27