CS 120 / Lab 4 - Feb 25 and 26, 2010 Allison Mankin, mankin@cs.jhu.edu 1. review pointers: Binky the Pointer video. Companion document with useful sample code. Binky sample code set up for lab examples: - lab4-binky.c is the code of what happens in the video - lab4.c below is the code of problem 3 in the Binky companion document Do go to the site as well as looking at these notes. It has all of the above for Java and C++, too. http://cslibrary.stanford.edu/104/ 2. a quick look at using gdb by running through the small linked list in lab4.c. Below you'll find the session output of gdb used for debugging lab4.c. The way this session uses gdb should be a useful model for looking at data structures and dereferencing links in your own code. Discussion questions about the gdb debugging session: - What is the significance of the number 0x100140? - Why is the same number printed for both y->next and z? - How should the programmer of lab4.c feel about this? - What is the point of the 'disable 1' command before the second 'run' command? 3. any questions on malloc? 4. memory usage and leakage - here's a unix tool, installed on the linux machines at JHU, that you should learn to use: valgrind 5. valgrind demo with example program - lab4-mem-o-matic.c the source of which is below. This program just takes as argument how many mallocs to do and then waits while you run tools to look at its memory usage. For you to do: valgrind your own pg3. Let's talk about the output. 6. next week's lab - midterm review! 7. reminder - come to TA office hours. We are happy to see you to talk about material from labs, assignments and class. We have hours at a lot of times, especially good to come to the hours prior to the evening before each assignment is due :-) 8. if time permits: how to be more secure in your use of putty/ssh through checking https://www.cs.jhu.edu/sshkeys/ Or take a look yourself - it has instructions. ========================================================== Example programs and GDB session ========================================================== /* lab4-binky.c - 02-2010 / A. Mankin Code for Binky's Crash from Companion Document to Binky the Pointer, Stanford CS Library, Document 106. Copyright Nick Parlante, 1999. */ #include main(void) { int* x; // Allocate the pointers x and y int* y; // (but not the pointees) x = malloc(sizeof(int)); // Allocate an int pointee, // and set x to point to it *x = 42; // Dereference x to store 42 in its pointee *y = 13; // CRASH -- y does not have a pointee yet y = x; // Pointer assignment sets y to point to x's pointee *y = 13; // Dereference y to store 13 in its (shared) pointee } ========================================================== /* lab4.c 02-2010 / A. Mankin Code for Problem 3 of Companion Document to Binky the Pointer. Stanford CS Library, Document 106. Copyright Nick Parlante, 1999. Incorporates snippet from list.h of cs120 samples. */ #include struct node { int value; struct node * next; } ; typedef struct node Node; int main (void) { { // Allocate the pointers. Node* x; Node* y; Node* z; // Allocate the pointees x = malloc(sizeof(Node)); y = malloc(sizeof(Node)); z = malloc(sizeof(Node)); // Put the numbers in the pointees x->value = 1; y->value = 2; z->value = 3; // Put the pointers in the pointees x->next = y; y->next = z; z->next = x; } } ========================================================== /* lab4-mem-o-matic.c 02-2010 / A. Mankin Run a lot of mallocs as a sample for looking at memory usage (and leakage) with valgrind. Usage: lab4-mem-o-matic <# of mallocs> */ #include #include #define RESPONSELEN 2 int main(int argc, char* argv[]) { int* x; int i, memarg, numargs; char userready[RESPONSELEN]; numargs = argc; if (numargs != 2) { printf("requires arg of number of mallocs to perform\n"); return (-1); } memarg = atoi(argv[1]); /* will malloc memarg times */ for(i=0; ivalue = 1; (gdb) next 35 y->value = 2; (gdb) next 36 z->value = 3; (gdb) next 39 x->next = y; (gdb) next 40 y->next = z; (gdb) next 41 z->next = x; (gdb) print y->value $1 = 2 (gdb) print /x y->next $2 = 0x100140 (gdb) print z $3 = (Node *) 0x100140 (gdb) disable 1 (gdb) run The program being debugged has been started already. Start it from the beginning? (y or n) y Starting program: /Users/mankiaj1/campus/cs120/lab4 Program exited normally. (gdb) ===============================================