# Debugging & Optimization

## **1. Debugging with GDB**

**GDB (GNU Debugger)** is a powerful tool for debugging C programs. It allows you to run your program step-by-step, inspect variables, and find out where things go wrong.

**Basic GDB Commands**

1. **Starting GDB**

   To start GDB, run the following command in your terminal:

   ```sh
   gdb ./your_program
   ```

   Replace `./your_program` with the path to your compiled executable.
2. **Setting Breakpoints**

   A breakpoint is a marker that tells GDB to pause execution at a certain point.

   ```gdb
   (gdb) break main
   ```

   This sets a breakpoint at the start of the `main` function.
3. **Running the Program**

   ```gdb
   (gdb) run
   ```

   This starts running your program until it hits a breakpoint or finishes.
4. **Stepping Through Code**

   To execute the program line by line:

   ```gdb
   (gdb) step
   ```

   To execute the program until the next breakpoint:

   ```gdb
   (gdb) next
   ```
5. **Inspecting Variables**

   To check the value of a variable:

   ```gdb
   (gdb) print variable_name
   ```
6. **Continuing Execution**

   To resume execution until the next breakpoint:

   ```gdb
   (gdb) continue
   ```
7. **Exiting GDB**

   To exit GDB:

   ```gdb
   (gdb) quit
   ```

**Example: Debugging a Simple Program**

```c
#include <stdio.h>

int main() {
    int a = 5;
    int b = 0;
    int c;

    c = a / b; // Division by zero
    printf("Result: %d\n", c);

    return 0;
}
```

**Debugging Steps:**

1. Compile with debugging information:

   ```sh
   gcc -g -o debug_example debug_example.c
   ```
2. Start GDB:

   ```sh
   gdb ./debug_example
   ```
3. Set a breakpoint at the line with the error:

   ```gdb
   (gdb) break 7
   ```
4. Run the program:

   ```gdb
   (gdb) run
   ```
5. Step through the code and inspect variables to find the issue.

***

## **2. Code Efficiency**

Optimizing code involves improving its performance and reducing resource usage. Here are some key principles for writing efficient code:

**1. Algorithmic Efficiency**

* **Choose the Right Algorithm**: The choice of algorithm greatly affects performance. For example, sorting algorithms like QuickSort or MergeSort are generally faster than BubbleSort for large datasets.

**2. Avoid Redundant Calculations**

* **Use Variables Wisely**: Store results of expensive calculations instead of recomputing them. For example, if you use the same calculation in a loop, compute it once and store the result.

  **Example:**

  ```c
  // Inefficient
  for (int i = 0; i < n; i++) {
      int result = expensiveFunction();
      // Use result
  }

  // Efficient
  int result = expensiveFunction();
  for (int i = 0; i < n; i++) {
      // Use result
  }
  ```

**3. Minimize Memory Usage**

* **Use Appropriate Data Types**: Use the smallest data type that fits your needs (e.g., `char` instead of `int` if you only need to store small integers).

**4. Optimize Loops**

* **Reduce Loop Overhead**: Minimize operations inside loops and avoid redundant calculations.

  **Example:**

  ```c
  // Inefficient
  for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
          // Code
      }
  }

  // Efficient
  for (int i = 0; i < n; i++) {
      // Code that does not depend on j
  }
  for (int j = 0; j < m; j++) {
      // Code that does not depend on i
  }
  ```

**5. Use Compiler Optimization**

* **Compile with Optimization Flags**: Use compiler flags to enable optimizations.

  **Example:**

  ```sh
  gcc -O2 -o optimized_program program.c
  ```

  The `-O2` flag enables a standard level of optimization.

***

#### **Summary**

* **Debugging with GDB**: Set breakpoints, run programs, step through code, inspect variables, and continue execution.
* **Code Efficiency**: Choose the right algorithms, avoid redundant calculations, minimize memory usage, optimize loops, and use compiler optimization flags.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://cs.d19.in/debugging-and-optimization.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
