CS120 Day 5: Arrays & Pointers; gprof, gcov ------------------------------------------------- - memory usage - registers - the "stack" - name table - the "heap" -automatic memory (arrays) are lost after a function return -dynamic memory is not lost -an array is a pointer except sizeof(array) actually works int * ptr = 0; - pointer variable is one that contains a memory address declaration: sometype * valptr creates variable valptr which is pointer to a location of sometype can be initialized to 0 or NULL (preferred, if #include ) - & address operator - returns address of whatever follows - * indirection or dereferencing operator - returns value being pointed to - both have unary precedence (one less than () and []) - can be used to simulate call by reference - pass variable with & to pass pointer to location, then use *var to get value - must use * as part of function prototype and definition parameter - swap example void swap(int * a, int * b) { int temp = *a; *a = *b; *b = temp; } git clone http://bitbucket.org/menohack/intermediate >>>> swap.c full example <<<< >>>> pointers.c sample code <<<< - pointers and arrays - can use almost interchangeably aptr = a; aptr = &a[0]; (a == &a[0]) a[3] == * (aptr + 3) /* parentheses necessary */ 3 is called the offset &a[3] == aptr + 3 - pointer arithmetic +, -, +=, -= for other pointers or integers - makes sense mostly for array subscripts - adds a unit the size of the ptr data type for each - ie, for each int unit added or subtracted, it goes one more or less memory location of that type = assignment works for pts of same types ==, etc. makes sense to compare ptrs within array, and compare to NULL - arrays of pointers - common use is array of strings - each string is pointer to a string array eg: char *suit[4] = {"Hearts", "Diamonds", "Clubs", "Spades"} - pointers to pointers - used to change value of pointer parameter - when passing to functions, if want pointer to change, must pass address of pointer (pointer to pointer) - go over this concept really well gprof -profiling - measuring the running time of each function -useful for optimization--improving the slowest function is the best option -add -pg compiler option to gcc -then run the executable to produce gmon.out -gprof executablename to show information gcov -test coverage - determines which lines of code were actually executed -good tests have 100% coverage of your code -add -fprofile-arcs -ftest-coverage options to gcc -turn off optimizations with -O0 (oh zero) or it can skip code -run the executable to produce .gcda file -"gcov " to list coverage for each source file -produces filename.c.gcov for each source file -.c.gcov files contain the number of times each line was executed ========== didn't get to this stuff ====================== - command line arguments main(int argc, char *argv[]) argc is count of arguments, including program name argv[0] is program name argv[i] is ith argument - const can be applied to any variable, in declaration or parameter list - added to ANSI C (not originally part of C) - should be used to protect data - as you write program, ask if variable should be modified - if not, then use const - for const variable: const int i - to make const pointer must say: int * const iptr = &i - must be initialized when declared (with = or as parameter) - eg: array is const ptr: *myarray ~ myarray[] - const ptr to const data: const int * const iptr - in general: read declarations from right to left to get it straight