Sie sind auf Seite 1von 8

Dereferencing in C

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

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

Dynamic vs. Static


l

static

allocated once
lifetime is entire program
immortal, never deallocated

canonical example, global variables


dynamic
allocated at run-time
lifetime is (possibly) brief
deallocated explicitly or implicitly
canonical example, local variables in subroutine
example

int x;

/* static */

void main() {
int y;

/* dynamic */

}
void foo(int z) { /* z is dynamic */
char ch[10000]; /* dynamic */
}

Heap vs. Stack


l

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
n visual depiction of frame

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;

/* dynamic stack storage */


/* dynamic stack storage */

str = malloc(100);
y = foo(23);
free(str);
}
int foo(int z) {
char ch[100];

/* allocates 100 bytes of dynamic heap storage */


/* deallocates 100 bytes of dynamic heap storage */
/* y and str deallocated as stack frame is popped */

/* z is dynamic stack storage */


/* ch is dynamic stack storage */

if (z == 23) foo(7);
return 3;
}
l

at the start of the program

/* z and ch are deallocated as stack frame is popped,


3 put on top of stack */

after the first call to foo

after the second call to foo

Copyright 1995. Curtis E. Dyreson. All rights reserved.

Copyright 1995. Curtis E. Dyreson. All rights reserved.

Das könnte Ihnen auch gefallen