Programming Studio #9 ECE 190
Programming Studio #9 Concepts: Functions review 2D Arrays GDB
Announcements EXAM 3 CONFLICT REQUESTS, ON COMPASS, DUE THIS MONDAY 5PM. NO EXTENSIONS, NO EXCEPTIONS.
Functions Same as subroutines Declared like variables: e.g., int func1(); char func2(); Return types Declaration specifies return type (i.e., output) func1() returns an integer func2() returns a character
Function Parameters Functions often take parameters Parameters Ex.: int larger_num(int num1, int num2); num1 and num2 are integers and are passed to the function larger_num, which returns an int Function larger_num then manipulates num1 and num2 to output larger of two Ex.: int a = larger_num(5,6);
Function: larger_num int larger_num( int num1, int num2 ); void main () { int x = 5; } int a = larger_num(x,6); /* return value (6) gets stored in a */ Call Declaration (must declare before call) Implementation larger_num() written outside main() int larger_num(int num1, int num2) { if( (num1-num2) > 0) return num1; else if( (num2 - num1) > 0) return num2; else return num1; /* both numbers are equal, therefore return either */ }
Pass by Value Pass arguments from caller to callee using actual values Value of expression is evaluated and pushed onto run-time stack to pass to the function int main() void Swap(int P, int Q) { { int A=3; int temp = P; int B=4; P = Q; Swap(A, B); Pass by Value } } Q = temp; Fail!
Pass by Reference By Value values not actually swapped Swap needs addresses of A and B to swap Pass by reference actually passes addresses int main() void Swap(int* P, int* Q) { { int A=3; int temp = *P; int B=4; *P = *Q; Swap(&A, &B); *Q = temp; } }
Two-Dimensional Arrays -Declaration: Just like regular arrays! Specify dimensions ex: int balances[11][6]; -Visualizing as a table, a is the row index, and b is the column index -Can be thought of as an array of arrays ex: balances[7] is an array of 6 integers (it is the 8 th array in the array of arrays) -This two-dimensional array must be put into a one-dimensional memory -C uses row-major order 9
Pointer-Array Equivalence Arrays are treated as pointers in most cases int array[5]; int* ptr = array; /* ptr and array can now be used interchangeably! */ The following pairs are equivalent: *a and a[0] a[10] and *(a + 10) These function declarations are equivalent: void func(float * arr); void func(float arr[]);
Using a debugger with C gdb is a debugger that is very similar to lc3sim Let s go through a short exercise to show you how gdb works. First: Download the code wget http://courses.engr.illinois.edu/ece190/discussion/spring11/ps9/test.c gcc g test.c o test Warning: We are intentionally compiling this without the usual flags to demonstrate what can go wrong if you don t. You must have all the required flags when working on mps.
Open up GDB (type gdb <exe> )
Type help to see a list of available commands
Type list to see your code!
Hitting enter runs the previously typed command again in this case it lists more lines.
Type list <num> to list the code around a certain line number (useful later).
Let s just run the code it crashed?
Run the backtrace command (or bt ) to get information about where the program crashed
It crashed at line 11, so let s look at line 11 Looks like we messed up scanf it should have been scanf ("%d", &i);
In order to change our code, we must quit the debugger. Open up test.c in VIM ( vim test.c ) and change line 11. Don t forget to recompile!
Let s open GDB back up ( gdb test ) and we can list the code to see it is fixed now. Always have to quit, fix, recompile, and launch gdb again to squash a bug.
If something is still wrong in your code, you can always set breakpoints to stop your code before that line executes. Type break 11 and run the code. It steps before executing line 11.
Type step to execute just that one line of code. To see the results print i and print num_scan will print out the variable values!
To finish running all of our code, type continue. Now the code will go to the next breakpoint, or finish executing if no breakpoints are left.
Debugging gdb reference Use gdb to debug C Compile using debug flag (i.e., -g) gcc g <C source file> -o <executable name> Run gdb in terminal gdb <executable name> Insert breakpoints break <line number> Run program run Step through program and into functions step Step through program next Continue program continue