Utp Lab Assignment - 1 Lab 1: GDB (And Functions) : Prompt G++ - G Program - CPP
Utp Lab Assignment - 1 Lab 1: GDB (And Functions) : Prompt G++ - G Program - CPP
Utp Lab Assignment - 1 Lab 1: GDB (And Functions) : Prompt G++ - G Program - CPP
Lab Exercise
Enter the program listed below:
1
1] /* Name
2] * Section \#
3] * Lab 4
4] * Date
5] */
6] #include<iostream>
7] using namespace
std;
8] int func(int);
9] const int X = 5;
2
When functions are called, new boxes are created for each local variable (including parameters), and when
a function returns, local variables’ boxes are destroyed.
For example, sample set of columns for hand simulating the lab program would look like:
X result answer y Output
Hand simulate the program. Using the first table at the back of the lab record the values of the
variables X, result, answer, and y at the following lines in the program: 11, 14, 16, 20, 23, 25. For some lines,
some variables will not be defined, record that information instead of the variable’s value.
Problem 2
Using gdb and the break, step, and print variable commands record the values of the variables X, result,
answer, and y at the following lines in the program: 11, 14, 16, 20, 23, 25, in the second table in the back
of the lab. (This could be done by putting lots of extra cout statements in the program, but for this lab we
will use gdb instead.) For some of these lines you will get an error because the variable doesn’t exist within
the scope of that line.
Do not go back and change the values in your first table.
Problem 3
Answer the following questions:
1. On line 14, what is the value of result and where did it come from? If you run the program several
times, does the value of result at line 14 ever change?
2. What are the values printed for X on lines 14 and 20? Why are the printed values of X different?
3. Compare the values in Table your first and second tables? We they different at any of the lines? If so
explain the mistake you made during the hand simulation.
Turn-in:
Turn in both tables and the answers to the three questions, you don’t need to turn in the
program or sample output.
3
Name:
11
14
16
20
23
25
11
14
16
20
23
25
4
Some Other Examples:
Example 1: Consider the following program
int main(int argc, char* argv[])
{
int x;
printf("Please enter an integer : ");
scanf("%d",x);
printf("the integer entered was %d \n", x);
return EXIT_SUCCESS;
}
The purpose of the program is to read an integer and print it out. But the program seg faults. Use gdb to find
the error.
6
Example 6:
Debug the program selection.cpp. This program tries to use the selection sort algorithm to sort an
array. However, it crashes with a dreaded Segmentation Fault. Usually, to debug a segfault, you want to run
the program under the debugger and let it crash. You can then use debugger commands to get information
about the state of the program when it crashed. The backtrace command can be particularly useful.
After you have fixed the segmentation fault, you might find that the program still doesn't sort the
array correctly. You should find and fix that bug too.
After selection.cpp correctly sorts the array, the program will go into an infinite loop when it tries to
use the binary_search function. Use the debugger to find and fix the problem. Since you don't usually know
where a program is going into an infinite loop, note that you can use Control-C inside gdb to pause the
program. You can then inspect the state of the program, install breakpoints, and so on.
#include <iostream>
int main() {