/* Lab 7 start * IMPORTANT: For lab 7, remember to build this with -g * g++ lab7.cpp -g * Then: * gdb a.out */ #include using namespace std; void q5func(int antelope){ int gorilla = antelope * antelope; cout << "q5func is printing " << gorilla << endl; // Put your breakpoint here } int main(){ /* Question 1: * Break the program on the line that assigns a value to c * Then print out the values of a, b, and c * After that, run next, and print the value of c again (it should change) * Put the values in your lab report */ int a, b, c; cout << "Enter a number: "; cin >> a; b = a * 2; c = a * a; // Break the program on this line /* Question 2: * This loop will calculate the sum of the array * Put a breakpoint into the loop * Then add a display for i, sum, and array[i] * Enter next repeatedly to watch the loop run * Note that you can push up arrow to get back the last command, so * when running next repeatedly, just do uparrow, enter, uparrow, enter, etc. * Add the final value of sum to your lab report */ int array[] = {5, 2, 10, 300, 26, 5}; int sum = 0; for(int i = 0; i < 6; i++) sum += array[i]; // Set a breakpoint on this line /* Question 3: * This loop calculates 10 factorial, usually stated 10! * Set a breakpoint on the line indicated below * Set i to 12: set (i=12) * Disable breakpoints: disable breakpoints * Continue, then quickly press ctrl+c to stop the program * print out i. * Question for your lab report: What was i? * If you did it right, i should be a large number. Why is it so large? */ long int result = 1; for(size_t i = 1; i != 11; i++) result *= i; // Set a breakpoint on this line cout << "10! is " << result << endl; /* Question 4: * This causes a segmentation fault! * place_for_array is a pointer, it doesn't point anywhere * It *could* point to an array, though. * Just run the program with this uncommented, and it'll here all on its own * Then print out place_for_array * Question for your lab report: What was the value of place_for_array? * Add the line below that sets place_for_array to array + 3 * Then set a breakpoint on the cout line below. * For the lab report: Post-fix, what is the value of place_for_array? */ int *place_for_array = 0; // Uncomment the following and see what happens // place_for_array = array + 3; cout << place_for_array[0] << endl; // After you fix it, put a breakpoint here /* Question 5: * This time we'll use the stack * Put a breakpoint in the function q5func. * Once the program is stopped, print out gorilla and &gorilla (address of gorilla) * Run backtrace or bt, and see that you're in a function * Run up, and you'll move up the stack to main * In main, print out &a, &b, &c, &result, and &sum. For the report, which is smallest? * What is the difference between the smallest address above, and &gorilla? * Feel free to use a calculator in hex mode to calculate the above * The result above is about the amount of memory required for a stack frame */ q5func(9137); return 0; }