12
12
12
Objective:
The main goal of this lab is to familiarize students with effective debugging techniques to identify and
correct errors and logic flaws in a program.
Debugging is a crucial aspect of the software development process in C++ as it plays a pivotal role in
ensuring the reliability and correctness of a program. Even the most seasoned developers encounter
errors in their code, and effective debugging is essential for identifying and rectifying these issues.
Debugging not only helps in eliminating errors but also enhances the overall quality of the code, making
it more efficient and maintainable.
1. Syntax Errors:
- Syntax errors occur when the code violates the rules of the C++ language. These errors are typically
detected by the compiler during the compilation phase. Examples include missing semicolons,
mismatched parentheses, or incorrect variable declarations.
2. Runtime Errors:
- Runtime errors occur during the execution of the program. They often lead to program crashes or
unexpected behavior. Examples include division by zero, accessing an out-of-bounds array element, or
dereferencing a null pointer.
3. Logic Errors:
- Logic errors are more subtle and challenging to detect. They occur when the program's logic is flawed,
leading to incorrect results. Examples include incorrect algorithm implementation, flawed conditional
statements, or improper loop control.
1. Print Statements:
- The use of print statements (e.g., `cout` statements) is a simple yet effective way to trace the flow of
the program and inspect variable values during runtime.
gdb ./your_program
(gdb) run
- Version control systems like Git allow developers to track changes in their codebase. This is valuable
for identifying when errors were introduced and for collaborating with other developers.
a. Print Statements:
Using print statements, such as `cout` in C++, is a fundamental and straightforward method to
understand the flow of the program and inspect the values of variables at different points during
runtime.
Example:
#include <iostream>
int main() {
int x = 5;
return 0;
Exercise: Given a C++ program with logical errors, use print statements strategically to identify and
correct the issues.
#include <iostream>
Using namespace std;
int main() {
int x = 5;
int y = 0;
y = x * i;
return 0;
In this example:
1. added print statements to display the values of variables (`i`, `x`, `y`) at different points in the program.
By strategically placing print statements and analyzing the output, you can identify and correct logical
errors in your C++ program. Keep in mind that this is a simplified example, and in a real-world scenario,
you may encounter more complex issues that require a more in-depth debugging approach, possibly
using a debugger or more sophisticated logging techniques.
b. IDE Debugging Tools:
Integrated Development Environments (IDEs) provide powerful debugging tools that assist in identifying
and fixing errors more efficiently. Key features include setting breakpoints, stepping through code, and
inspecting variable values in real-time.
Example:
3. Execution will pause at the breakpoint, allowing you to inspect variable values.
Exercise: Debug a C++ program using breakpoints and step-by-step execution in your preferred IDE.
#include <iostream>
int main() {
int x = 5;
int y = 0;
y = x * i;
return 0;
To debug a C++ program using breakpoints and step-by-step execution in Dev C++, follow these steps:
1. Open the Project:
2. Set a Breakpoint:
- Click on the left margin of the editor window at that line number. This sets a breakpoint.
- Ensure that you are compiling your program with debugging information. In Dev C++, this is typically
done by selecting a "Debug" build or ensuring that debugging information is included in your
compilation settings.
4. Start Debugging:
- The program will start, and execution will pause at the line where you set the breakpoint.
6. Inspect Variables:
- You can now inspect the values of variables by hovering over them or by using the Watches window.
- Use the debugging toolbar or the keyboard shortcuts to step through the code:
- Step Into (F7): Executes the current line and moves to the next line. If the current line is a function
call, it enters the function.
- Step Over (F8): Executes the current line. If the current line is a function call, it executes the entire
function and moves to the next line in the calling function.
- Step Out (Shift + F8): Executes the remaining lines of the current function and returns to the
calling function.
8. Continue Execution:
- If you want to run the program until the next breakpoint, select "Continue" from the debugging
toolbar or press F9.
- The call stack window shows you the current function calls. You can use this to trace the flow of your
program.
- Use the Watch window to monitor the values of specific variables during execution.
- As you step through the code, you may identify the issues causing unexpected behavior. Fix them and
continue debugging until your program runs correctly.
- Once you've fixed the issues, stop debugging by selecting "Stop Debugging" from the "Run" menu or
by pressing Shift + F5.
c. Logging:
Logging involves strategically placing log statements in the code to capture information about the
program's behavior during runtime. This can help track the flow of execution and identify unexpected
behavior.
Example:
#include <iostream>
#include <fstream>
logFile.close();
}
int main() {
logMessage("Program started");
return 0;
Exercise: Implement logging in a C++ program and analyze the log output to identify and fix issues.
#include <iostream>
#include <fstream>
// Logging function
logfile.close();
int main() {
// Initialize variables
int x = 5;
int y = 0;
logMessage("Attempting division...");
if (y != 0) {
int result = x / y;
} else {
return 0;
Follow the steps below to compile and run this program in Dev C++:
- Ensure that you are compiling the program with debugging information enabled.
Attempting division...
By following these steps, you can implement logging in a C++ program using Dev C++, allowing you to
analyze log output to identify and fix issues.
Task: