Professional Documents
Culture Documents
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))
*/
*/
*/
*/
*/
*/
*/
*/
Pointer Assignment
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.
1. dereference rhs in pointer assignment, so ptr now points to the same place as ptr_2
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 = a;
fp = foo;
/* ptr = dereference(ptr_2) */
/* ptr = ptr_2 */
/* fp = foo */
Storage Model
l
l
l
l
static
allocated once
lifetime is entire program
immortal, never deallocated
int x;
/* static */
void main() {
int y;
/* dynamic */
}
void foo(int z) { /* z is dynamic */
char ch[10000]; /* dynamic */
}
heap
stack
parameters
return address -- where to begin execution when function exits
n dynamic link -- pointer to caller's stack frame
n static link -- pointer to lexical parent (for nested functions)
n return value -- where to put the return value
n local variables
n local work space -- for temporary storage of results
function call -- push stack frame
function exit -- pop stack frame
visual depiction of stack calls
n
n
example
int x;
/* static storage */
void main() {
int y;
char *str;
str = malloc(100);
y = foo(23);
free(str);
}
int foo(int z) {
char ch[100];
if (z == 23) foo(7);
return 3;
}
l