Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.71 KB | None | 0 0
  1. * Lab 7 - GDB
  2. In this lab, I was looking for an error inside the q1 file provided by taking advantage of the gdb
  3. debugger.
  4.  
  5. ** Function
  6. Just from a quick glance at the program I can tell it is supposed to take in 5 cin inputs, that
  7. will be stored inside an array. Then using the minval function, it is supposed to return the smallest
  8. of the inputs
  9.  
  10. #+BEGIN_SRC cpp
  11.  
  12. for (i=0; i<=5; i++){
  13. cin >> tmp;
  14. if (i=3)
  15. array[i]=tmp;
  16. else
  17. array[i]=10*tmp;
  18.  
  19. m = minval(array ,5);
  20. cout << "the smallest is " << m << endl;
  21.  
  22. #+END_SRC
  23.  
  24. However when I run the program, I get into an infinite loop of cin inputs. I thought at first it
  25. could be an error on my side but when I run gdb and continue the steps, it is indeed looping
  26. through the for loop over and over with no end, so there is a bug present.
  27.  
  28. I started off fixing the error that minval(a, 5) is supposed to be minval(array, 5)
  29.  
  30. #+BEGIN_SRC cpp
  31. minval(a, 5);
  32. //changed code
  33. minval(array, 5);
  34. #+END_SRC
  35.  
  36. then I compiled using
  37. #+BEGIN_QUOTE
  38. g++ -g q1.cpp
  39. #+END_QUOTE
  40.  
  41. ** Process
  42. I began by launching ddd (the visual representation helps) and analyzing the
  43. variables present. There weren't really any crashing or segfaults so I didn't put any break points.
  44. I started by looking at the values of "i" by typing "print i", from lines 20-23 as they were
  45. repeating. It wasn't incrementing properly.
  46.  
  47. 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
  48.  
  49. I did
  50. #+BEGIN_QUOTE
  51. set var array[0] = 1
  52. #+END_QUOTE
  53. to set variables for the array
  54. then I did print array to view my array
  55. (the code info local was more useful since it showed all the variables)
  56. then I made i = 5 by doing
  57. set var i = 5 so that it will finally go on to the next part of the function
  58. now that it did the minval function, it returned the highest vavlue of the array.
  59. Now I saw that my operator was "> " inside the minval function , so then I changed it to
  60. "<" and recompiled.
  61.  
  62. I started over just to make sure that the program was running fine which it did.
  63. the only problem left was to fix the infinite loop.
  64. I looked at the code again and I felt so stupid that the if function had 1 equals sign not 2!
  65.  
  66. #+BEGIN_SRC cpp
  67. if(i=3)
  68. //changed code
  69. if(i==3)
  70. #+END_SRC
  71. that means it was assigning i = 3 every time so it was looping.
  72.  
  73. after i fixed this I ran into another problem, it always returned 0!
  74. I ran gdb again and in the minval function I noticed currmin wasn't defined so under the code
  75.  
  76. #+BEGIN_SRC cpp
  77. if(A[i] < currmin)
  78. #+END_SRC
  79.  
  80. It' wasnt comparing to any value so that is probably why it was returning 0.
  81. So i added the code
  82.  
  83. #+BEGIN_SRC cpp
  84. currmin = A[0]
  85. #+END_SRC
  86.  
  87. The code was working so far however, it wasn't comparing the last value, then I came to the
  88. realization
  89. #+BEGIN_SRC cpp
  90. m = minval(array , 5);
  91. #+END_SRC
  92. by default had 5 values, when there were 6 inputs since it counts for 0 as well.
  93.  
  94. so the last change I did was
  95. #+BEGIN_SRC cpp
  96. m = minval(array , 6);
  97. #+END_SRC
  98.  
  99. I also added cout << "This value won't be multiplied by 10:"
  100. So that I won't mistake the value that isn't multiplied by 10.
  101.  
  102. **Final Results
  103. ***Sample Input/Output
  104. *Input*
  105. #+BEGIN_SRC
  106. 1
  107. 2
  108. 3
  109. This value won't be multiplied by 10: -799
  110. -8
  111. -80
  112. #+END_SRC
  113.  
  114. *Output*
  115. #+BEGIN_SRC
  116. The smallest value is -800
  117. #+END_SRC
  118.  
  119. ** Thoughts
  120. It was difficult to approach at first since I wasn't that familiar with gdb but playing
  121. around with the cheat sheet some of the things were kind of self explanatory so I think I kind
  122. of understand the debugging process more now and why it helps. Then again some people fixed the
  123. code apparently without the use of a debugger *cough Anna, but that doesn't always happen especially
  124. when code gets more complicated. Therefore debuggers can really help a lot
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement