home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Programming / C / SASC6571.LZX / examples / cxx / READ.ME < prev    next >
Encoding:
Text File  |  1996-12-24  |  5.7 KB  |  193 lines

  1. Debugging C++ Programs
  2.  
  3. Introduction
  4.  
  5. This chapter contains an example of debugging a C++ program.
  6. The example program is in the sc:examples/cxx .  This directory
  7. contains two programs:
  8.  
  9. chess1.cxx is a basic chess program that declares two classes:
  10. chessPiece  and chessBoard . The chessPiece  class
  11. describes the pieces used in a game of chess. The chessBoard 
  12. class describes the board on which the chess pieces
  13. are placed and moved. The chessBoard  class contains a
  14. data member, board , which is an array of
  15. pointers to chessPiece  objects.
  16.  
  17. chess1.cxx   contains a logic error.  The instructions
  18. in this chapter show you how to use CodeProbe to find the error.
  19.  
  20. chess2.cxx contains a corrected version of chess1.cxx .  You can
  21. compile, link, and run chess2.cxx  if you want to see what
  22. chess1.cxx  should print after you have fixed its logic
  23. error.
  24.  
  25. Appendix 2, "Source Code for Debugger Examples," contains the source
  26. code for chess1.cxx .
  27.  
  28. Running The Example
  29.  
  30. To compile and link this example, enter the following command at
  31. the Shell prompt:
  32.  
  33.    sc debug=ff pname=chess link sc:examples/cxx/chess1.cxx
  34.  
  35. This command creates an executable module named chess  in your
  36. current directory.  To run the program from the Shell prompt, enter:
  37.  
  38.    chess
  39.  
  40. The program first instantiates a chessBoard  object
  41. called gameboard .  The
  42. chessBoard  constructor then instantiates the appropriate
  43. chessPiece  objects needed in a chess game. Each position in the
  44. chessBoard  object's 8x8 array points to the correct
  45. chessPiece  object at the start of chess game.
  46.  
  47. Next, the program starts the game with the Sicilian opening and
  48. displays the chess board.  However, the pieces that were moved are
  49. moved to the wrong sides of the chess board.
  50.  
  51. Finally, the gameboard  object is a local object to the function
  52. main , so its scope ends when function main  returns. board  is
  53. a chessBoard  object, so it is destructed when the program ends.
  54. The chessBoard  destructor frees all of the chessPiece  objects
  55. to which its data member board  points.
  56.  
  57. Debugging The Program
  58.  
  59. To run chess  under the control of the
  60. debugger, enter the cpr  command at the Shell prompt:
  61.  
  62.    cpr chess
  63.  
  64. Since the chess program is setting up the board correctly, but
  65. pieces are being moved to the wrong places, the error is probably
  66. in the chessBoard::movePiece  function.  For C++ programs,
  67. you can identify a member function by specifying its class and
  68. name separated by a double colon:
  69.  
  70.    > break chessBoard::movePiece
  71.  
  72. Now, run the program until it encounters the breakpoint:
  73.  
  74.    > go
  75.  
  76. CodeProbe stops at the first line of the
  77. chessBoard::movePiece  function.
  78.  
  79. First, display the coordinates from which the next piece will be
  80. moved: old_r  and old_c :
  81.  
  82.    > d old_r
  83.    1 (0x1)
  84.    > d old_c
  85.    4 (0x4)
  86.  
  87. In the Sicilian opening, the first move is Pawn to King 4 (P-K4).
  88. Since white always moves first, the piece that is moved should
  89. be the white pawn.  You want to verify that the piece identified
  90. at coordinates is the white pawn.
  91.  
  92. To display the contents of any class data member within a
  93. member function, treat the data member like a global variable.
  94. CodeProbe uses the this  pointer to display the data member of
  95. the correct object.  To find out what piece will be moved,
  96. enter the following display  command:
  97.  
  98.    > d *board[1][4]
  99.  
  100. CodeProbe prints the data members of the chessPiece  object
  101. pointed to by board[1][4]  :
  102.  
  103.    struct chessPiece {
  104.       name = "pawn"
  105.       color = 'w' 119 (0x77)
  106.       code = 'p' 80 (0x50)
  107.       value = 1 (0x1)
  108.    }
  109.  
  110. You now know that the correct piece will be moved, so
  111. execute the program until the chess piece has been moved from its
  112. old position to its new position.  The code that moves the chess
  113. piece is:
  114.  
  115.    board[new_c][ new_r]  = board[ old_r] [ old_c] ;
  116.  
  117. To execute chess  past this line, you enter:
  118.  
  119.    > ps 4
  120.  
  121. The coordinates of the pawn's new position should be
  122. board[ 3] [ 4]  .  To verify that the
  123. correct coordinates are passed into chessBoard::movePiece,
  124. display the values of new_r  and new_c :
  125.  
  126.    > d new_r
  127.    3 (0x3)
  128.    > d new_c
  129.    4 (0x4)
  130.  
  131. These coordinates are correct.
  132. To determine if the white pawn was moved to position
  133. board[ 3] [ 4]  , enter the display command:
  134.  
  135.    > d *board[ 3] [ 4]
  136.    0x0
  137.  
  138. This position on the board is NULL .  The white pawn was
  139. moved somewhere else. You need to find out where the white pawn
  140. was moved to.
  141.  
  142. First, delete the current breakpoint, run the program to completion,
  143. and restart the chess  program:
  144.  
  145.    > bc *
  146.    > go
  147.    Program exited with code 0.
  148.    > restart
  149.    Starting "chess"
  150.    chess:\t:chess..c\main 109
  151.  
  152. Then, set a breakpoint in chessBoard::movePiece  that
  153. stops the program when color  is w  and
  154. code  is P .  Use the slash (\ )
  155. character to continue the command line:
  156.  
  157.    > break chessBoard::movePiece \
  158.    >> when(board[ old_r] [ old_c] ->color == 'w' && \
  159.    >> board[ old_r] [ old_c] ->code == 'P')
  160.  
  161. You can find out what position the white pawn is being moved to by
  162. displaying the values of new_r  and new_c  again:
  163.  
  164.    > d new_r
  165.    3 (0x3)
  166.    > d new_c
  167.    4 (0x4)
  168.  
  169. The coordinates to which the white pawn was supposed to have
  170. has been moved are correct, but the instruction that uses these
  171. coordinates uses them in reverse order.
  172. The white pawn should be moved to board[ 3] [ 4]  ,
  173. not board[ 4] [ 3]  , which indicates that the
  174. assignment statement shown previously is incorrect:
  175.  
  176.    board[ new_c] [ new_r]  = board[ old_r] [ old_c] ;
  177.  
  178. Before you can fix the error in the program, you need to run the
  179. program to completion, and then quit the debugger.
  180.  
  181.    > go
  182.    Program exited with code 0.
  183.    > quit
  184.  
  185. Edit the chess1.cxx  file, and change the assignment statement
  186. on line 86 in chessBoard::movePiece  to read:
  187.  
  188.    board[ new_r] [ new_c]  = board[ old_r] [ old_c] ;
  189.  
  190. Then, compile the example as described under "Running The Example."
  191. Run the program again.  Now the board prints correctly.
  192.  
  193.