====================================== Notes for Lecture 7 - February 5, 2008 ====================================== * Strings ** A C-string *** Is sequence of non-null characters followed by a null bype (character '\0' == 0). *** Stored in an array of type char []. *** Elements are individual characters Example: type char mystr[8] = "the cat" creates an array of 8 bytes: mystr[0] == 't' mystr[1] == 'h' mystr[2] == 'e' mystr[3] == ' ' mystr[4] == 'c' mystr[5] == 'a' mystr[6] == 't' mystr[7] == '\0' == 0 Array elements can be changed, so after mysr[4] = 'r', the string in the array is "the rat". Can be printed using %s format specifier, e.g. printf( "%s\n", mystr ); (See demos-07/1-chararray) * C storage model ** Virtual memory *** Organized as a sequence of sequentially numbered bytes *** Byte number is its address *** Program and data lives in virtual memory *** Virtual memory is mapped to physical memory by the OS *** Segmentation fault results from illegal attempt to access VM ** C variables *** Correspond to blocks of virtual memory *** Allocated in one of three storage areas: stack, dynamic, static *** Three areas have different lifetimes ** Stack memory *** Memory allocated automatically when variable declared. *** Memory released when control leaves allocating block. ** Dynamic memory *** Memory allocated explicitly with malloc(). *** Memory freed explicitly with free(). *** Access is only through pointers. ** Static *** Memory allocated when program begins. *** Memory freed when program terminates. * Pointers ** References A reference is an index into virtual memory. It allows access to storage. An ordinary variable x can be considered to be a named reference to a data object consisting of sizeof(x) bytes. The memory addessed is called the target of the reference. References can be stored and manipulated just like other data. Variables that can store reference values are called pointers. ** Pointer type Values are references. Type also tells how to interpret the bytes at the destination. Example: int* is the type of a pointer to an int. Special type void* says only that the value is a pointer. Must be cast to a non-void pointer type before use. ** Pointer variables A variable of a pointer type is called a pointer variable. The word "pointer" is used ambiguously to denote either a pointer variable or the contents of a pointer variable, i.e. a reference. Similar remarks apply to the term "integer", which may refer to a variable (as in "x is an integer") or to its contents. ** Pointer variable declaration *** Similar to other type declarations Example: int* p. *** Warning Multiple declarations don't behave as expected in C. Example: int* p, q; declares p to be a pointer to int but q to be an int. Less confusing when written: int *p, q; but that obscures the fact that * really belongs to the type. ** Reference creation *** With & operator If x is a variable of type T, then &x is a pointer of type T*. *** With malloc() malloc() allocates a block of dynamic storage and returns a pointer to it of type void*. ** Use of pointer variables and references *** The * operator and references If e is a reference of type T*, then *e is a reference of type T. The expression *e produces an unnamed reference. (*e) can be used just like a variable, e.g., *e = x+1; *** Use of pointer variables **** Pointer variables work just like other variables Example: int *p, *q; ... p = q; assigns the value (reference) in q to p. Afterwards, p and q contain the same reference. Example: int *p; int x; p = &x; puts a reference to x into p. Example: int *p, int x: *p = x; puts the integer value of x into the target of the reference in p. ** Reference arithmetic *** Addition and substraction are defined on references and integers *** Assume e, f are references of type T, k is an integer *** e+k is a reference of type T It refers to the memory at address(e) + k*sizeof(T) *** e-k is a reference of type T It refers to the memory at address(e) - k*sizeof(T) *** e-f is an integer Its value is (address(e)-address(f))/size(T).