int z;
int x[3];
int *ptr;
/* meaning making dereferences explicit */
z = 3; /* z = 3 */
z = z; /* z = dereference(z) */
ptr = &z; /* ptr = &z */
z = (int) ptr; /* z = dereference(ptr) */
z = *ptr; /* z = dereference(dereference(ptr)) */
x[z] = 3; /* x[dereference(z)] = 3 */
foo( z ); /* foo(dereference(z)) */
Assignment where the required reference is a reference to a pointer variable
and the value is itself a reference.
ptr_2 = &some_record;
ptr = ptr_2; /* pointer assignment */
Two interpretations.
- dereference rhs in pointer assignment, so ptr now points to the
same place as ptr_2
- do not dereference rhs in pointer assignment, so ptr now points
to ptr_2
C has both! Draw pictures int foo() {
int x, a[5], *ptr, *ptr_2, (*fp)();
ptr_2 = &x;
ptr = ptr_2; /* ptr = dereference(ptr_2) */
ptr = a; /* ptr = ptr_2 */
fp = foo; /* fp = foo */
- birth - when allocated
- death - when deallocated
- lifetime - time between birth and death
- run-time storage manager/memory manager
- allocates objects
- deallocates objects
- garbage collects when memory becomes fragmented
- visual depiction of run-time storage
- heap
- freelist -- list of free space
- on allocation -- memory manager finds space and marks it as used
changing freelist
- on deallocation -- memory manager marks space as free changing freelist
- memory fragmentation -- memory fragments into small blocks over lifetime
of program
- garbage collection -- coalesce fragments, possibly moving objects (must
be careful of pointers when moving!)
- stack
- clean and efficient support for nested functions and recursion
- central concept is stack frame (also called activation record), includes
- visual depiction of frame
- parameters
- return address -- where to begin execution when function exits
- dynamic link -- pointer to caller's stack frame
- static link -- pointer to lexical parent (for nested functions)
- return value -- where to put the return value
- local variables
- local work space -- for temporary storage of results
- function call -- push stack frame
- function exit -- pop stack frame
- visual depiction of stack calls
- example
int x; /* static storage */
void main() {
int y; /* dynamic stack storage */
char *str; /* dynamic stack storage */
str = malloc(100); /* allocates 100 bytes of dynamic heap storage */
y = foo(23);
free(str); /* deallocates 100 bytes of dynamic heap storage */
} /* y and str deallocated as stack frame is popped */
int foo(int z) { /* z is dynamic stack storage */
char ch[100]; /* ch is dynamic stack storage */
if (z == 23) foo(7);
return 3; /* z and ch are deallocated as stack frame is popped,
3 put on top of stack */
}
- at the start of the program
- after the first call to foo
- after the second call to foo
Copyright © 1995. Curtis E. Dyreson. All rights reserved.
http://www.cs.jcu.edu.au/ftp/web/teaching/Subjects/cp2050a/current/foils/sep5/sep5.html