Lecture 11, ECE264, Spring 2012, Prof. Lu
2/14 CLASS NOTES _ Kailu Song 1.Quiz(Recursion)
int f(int n) {
int a, b, c; if (n == 1) { return 2; } if (n == 2) { return 3; } a = f(n - 1); b = f(n - 2); c = a + b; return c;
} int main(int) {
int s ; s = f(4); return EXIT_SUCCESS;
}
Draw the call stack for it Frame symbol address value function s 200 ?=5
c 218 ? b 217 ? a 216 ? n 215 3-1=2(!=1/2 so need get into the function) value address 210 return address
function c 212 ?=5
b 211 ?=2 a 210 ?=3 n 209 3(!=1/2 so need get into the function) value address return address(return after calling a = f(n-1))
main c 206 ?
b 205 ? a 204 ?=5(then do b) n 203 4(all the functions are gone and return to the main function after get the value of n) value address 200(based on the address value of s) return address(after calling f(4))
2.gdb function: go to terminal :
gcc -g filename -o filename
then "emacs" ESC x gdb (press enter) [after this, there will be another window show up] (gdb) b main : break point (gdb) list (gdb) r : run (ex. (gdb) r args"ex3 517 data1") (gdb) s : step (enter a function) (gdb) n:nest (execute the function to the next line) (gdb) bt : backtrace(call stack, there will show up the address) how to find the segment fault: gdb,then execute, then bt (gdb) print var (get the value at that point)
3.announment about the computer-based exam: open book, open notes, internet access, 3/1 on-class two question, cannot compile get zero unlimited submission but only last submission be counted to grade
4. structure (new topic) how to write down a structure typedef struct {
int x; int y; int z;
} Vector; /* do not forget ; */
int main(int argc, char * argv[]) {
Vector v; v.x = 1; v.y = 2; v.z = 3;
}
the useful of the structure:to make your code easily to change according to the requirement of customer.