Lecture 8 -------------------- Announce ---------------- - hw5 due - hw6 posted, due monday - hw1,2,3 grades, sorry - project1 extra credit clarification Today ------------- - More Pointers - UNIX Process Memory - Dynamic Memory - C Structures Review ------------ - pointer defn - pointer uses - access variables outside curr scope - 'return' more than one value - 'pass' more than one value - dynamic memory allocation More Pointers ----------------- - array and pointer relationship - name of array pointer to addr of memory - p = a; - a = p <-- NO! - ddim arrays and pointer relationship - say wanted to store all of the months - char months[12][10]; - char *months[12]; - important to DRAW these data structures - why pointers? save memory and time, can reshuffle! - pointers and types - all pointers store address, which is some range - why are they typed? - pointers must be typed for dereferencing purposes. - POWERFUL pointer uses: pointers can be used to make a program dynamic and generic - can use to grab arbitrary chunks of memory - can use to make variable function calls - function pointers - ex: built in qsort - need to compare two elements during computation - elements could be numbers - elements could be strings - could want to sort small to big, big to small - write different qsort for all known types? - we can define our OWN types, then what? - write a compare function, pass it to qsort as a parameter ex: qsort(void *base, int num, int size, int (*compar)(void *, void *)) { int a; a = compar(base[0], base[1]); } - dynamic memory pointers Dynamic Memory ------------------------------ - motivation: we've been statically allocating memory! - picture of memory used for a process ** stack heap data segment text segment - works fine when we have global/constant variables and functions that operate on them - also works fine when we use variables within the scope of functions - what happens when we need more memory? - all our variables are defined in the data seg or in the text/stack area - we have to borrow memory from the heap! - malloc function - returns a void * - untyped pointer, your resp to cast - takes the number of bytes to allocate - returns a NULL on error (no more memory) ex: enough space for 8 integers; int *myarray; myarray = malloc( ? ); sizeof(int) * 8; - why is this cool? you can put variables into malloc!! Memory Management ---------------------- - stack picture - call a function - new stack frame - function calls malloc - function exits - eek! - or just redefine the ptr, eek! - In C, programmer is responsible for doing memory management - must care for your pointers and free unused memroy yourself - free(ptr); C Strings ----------------------------- - quickly, c strings are char arrays - usually we use char * to manage strings (variable length) - programmers have coded many useful functions to deal with strings - use '\0' to terminate by convention - strdup(), strlen(), strcat(), strcpy(), strstr(), strcmp(), etc C Structures ----------------------------- - arrays are OK but all the same type! - another way to encapsulate multiple variables - struct ex: struct playing_card { int value; char color; char *suit; }; - defined outside main/other functions - once defined, can now be declared just like other variables struct playing_card mycard; struct playing_card deck[52]; - accessing struct members mycard.value = 10; mycard.color = 'R'; mycard.suit = strdup("hearts"); - accessing struct members via a struct pointer struct playing_card *cptr; cptr = &mycard; cptr->value = 5; cptr->color = 'B'; cptr->suit = strdup("clubs"); - operations on structs are illegal as they are undefined! - +, -, /, * - assignments are legal, but watchout! ** draw picture - ask them what happens struct playing_card newcard; newcard = mycard; - better to define a function to copy contents of one struct to another card_copy(struct playing_card *dest, struct playing_card *source) { dest->value = source->value; dest->color = source->color; ??? dest->suit = strdup(source->suit); }