Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- * Lab 7 - GDB
- In this lab, I was looking for an error inside the q1 file provided by taking advantage of the gdb
- debugger.
- ** Function
- Just from a quick glance at the program I can tell it is supposed to take in 5 cin inputs, that
- will be stored inside an array. Then using the minval function, it is supposed to return the smallest
- of the inputs
- #+BEGIN_SRC cpp
- for (i=0; i<=5; i++){
- cin >> tmp;
- if (i=3)
- array[i]=tmp;
- else
- array[i]=10*tmp;
- m = minval(array ,5);
- cout << "the smallest is " << m << endl;
- #+END_SRC
- However when I run the program, I get into an infinite loop of cin inputs. I thought at first it
- could be an error on my side but when I run gdb and continue the steps, it is indeed looping
- through the for loop over and over with no end, so there is a bug present.
- I started off fixing the error that minval(a, 5) is supposed to be minval(array, 5)
- #+BEGIN_SRC cpp
- minval(a, 5);
- //changed code
- minval(array, 5);
- #+END_SRC
- then I compiled using
- #+BEGIN_QUOTE
- g++ -g q1.cpp
- #+END_QUOTE
- ** Process
- I began by launching ddd (the visual representation helps) and analyzing the
- variables present. There weren't really any crashing or segfaults so I didn't put any break points.
- I started by looking at the values of "i" by typing "print i", from lines 20-23 as they were
- repeating. It wasn't incrementing properly.
- I thought what if I set the variables in gdp then make i = 5 since it's not reaching 5 what would happen. all the i values were 3
- I did
- #+BEGIN_QUOTE
- set var array[0] = 1
- #+END_QUOTE
- to set variables for the array
- then I did print array to view my array
- (the code info local was more useful since it showed all the variables)
- then I made i = 5 by doing
- set var i = 5 so that it will finally go on to the next part of the function
- now that it did the minval function, it returned the highest vavlue of the array.
- Now I saw that my operator was "> " inside the minval function , so then I changed it to
- "<" and recompiled.
- I started over just to make sure that the program was running fine which it did.
- the only problem left was to fix the infinite loop.
- I looked at the code again and I felt so stupid that the if function had 1 equals sign not 2!
- #+BEGIN_SRC cpp
- if(i=3)
- //changed code
- if(i==3)
- #+END_SRC
- that means it was assigning i = 3 every time so it was looping.
- after i fixed this I ran into another problem, it always returned 0!
- I ran gdb again and in the minval function I noticed currmin wasn't defined so under the code
- #+BEGIN_SRC cpp
- if(A[i] < currmin)
- #+END_SRC
- It' wasnt comparing to any value so that is probably why it was returning 0.
- So i added the code
- #+BEGIN_SRC cpp
- currmin = A[0]
- #+END_SRC
- The code was working so far however, it wasn't comparing the last value, then I came to the
- realization
- #+BEGIN_SRC cpp
- m = minval(array , 5);
- #+END_SRC
- by default had 5 values, when there were 6 inputs since it counts for 0 as well.
- so the last change I did was
- #+BEGIN_SRC cpp
- m = minval(array , 6);
- #+END_SRC
- I also added cout << "This value won't be multiplied by 10:"
- So that I won't mistake the value that isn't multiplied by 10.
- **Final Results
- ***Sample Input/Output
- *Input*
- #+BEGIN_SRC
- 1
- 2
- 3
- This value won't be multiplied by 10: -799
- -8
- -80
- #+END_SRC
- *Output*
- #+BEGIN_SRC
- The smallest value is -800
- #+END_SRC
- ** Thoughts
- It was difficult to approach at first since I wasn't that familiar with gdb but playing
- around with the cheat sheet some of the things were kind of self explanatory so I think I kind
- of understand the debugging process more now and why it helps. Then again some people fixed the
- code apparently without the use of a debugger *cough Anna, but that doesn't always happen especially
- when code gets more complicated. Therefore debuggers can really help a lot
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement