Chapter Contents


Debugging C++ Programs

C++ Debugging Example

This section provides an example of running the debugger with a C++ program. Some of the features the example illustrates include

The important thing to remember is that debugging C++ programs with the SAS/C Debugger is virtually the same as debugging C programs. The debugger looks and feels the same, and in general the commands are the same. When you have completed this example debugger session, you should be ready to debug your own C++ programs.

Note:    This example requires you to allocate the DDname DBGSLIB to the location of your standard header files. See the SAS/C Library Reference, Volume 1 for more information about the DBGSLIB DDname.  [cautionend]

Example Source Code

Here is the source code for the example. The program declares class X , which includes one data object, two constructors, one member function, and a destructor. The member function multiplies the data object by 2. The destructor checks the value of the data object and prints an error message if the data object is not between 7 and 10. The main function uses the constructors to create several instances of class X, calls the member function four times (once for each instance of class X ), and then deletes the instances of class X .

#include <iostream.h>
class X
public: int i;
      // first X constructor
   X(int ia)
      i = ia;
      // second X constructor
   X(int ib, int jb)
      i = ib + jb;
       // X member function myfunc()
   void myfunc()
      i *= 2;
      // X destructor
      int id;
      id = i / 2;
      if (id < 7 || id > 10)
      printf("Error - Out of range\n");
int main()
   X *x1 = new X(7);
   X *x2 = new X(6,2);
   X *x3 = new X(9);
   X *x4 = new X(9,1);
   delete x1;
   delete x2;
   delete x3;
   delete x4;
   return 0;

Sample Debugger Session

In this example debugger session, you use the break, go, query, drop , and on debugger commands to complete the program. The numbered steps that follow tell you what to type in and show the results of your commands in the various debugger windows.

You first need to translate, link, and run the sample program. Be sure to specify the debug option when you translate.

When you run your program, the debugger stops on a line of code in iostream.init function, as described in Debugging Initialization and Termination Functions.

Issue the following debugger commands in sequence. Debugger commands are issued after the Cdebug: prompt located in the Command window at the bottom of the screen.

  1. break "X::X"(0) entry

    This command sets breakpoints at entry to all constructors for X . The subscript of 0 is necessary because the constructors are overloaded functions. Because the function name has a multitoken name, double quotes are necessary.

  2. break main 37


    These two commands first set a breakpoint at line 37 of the main function and tell the debugger to advance to the first breakpoint. The debugger stops on line 37 of main .

  3. go

    The program proceeds until it enters the first constructor.

  4. go

    Yet another go causes the debugger to stop at the entry to the next constructor.

  5. query

    This command requests the Log window to show all actions and monitors in effect. Two breakpoints are shown; of special interest is the first breakpoint, which shows the subscript of 0, indicating a breakpoint is in effect for all instances of the overloaded constructor, X.

  6. drop 1
    break "X::X"(1) return
    break "X::X"(2) return
    on myfunc entry print i
    on myfunc return print i
    b "X::~X" return

    The drop command drops breakpoint #1 (it is no longer necessary). Next, set breakpoints on the return of both versions of the overloaded constructor, using the subscripts 1 and 2. The two on commands tell the debugger to print the value of the X member function i on the entry to and return from the myfunc member function. The next command sets a breakpoint on the return of the destructor ( b is an abbreviation for break ). Finally, the query command shows all the actions and monitors that are in effect. Of special interest are the third and fourth breakpoints. These breakpoints show the prototypes for the first and second constructors (the first takes a single int ; the second takes two int s).

  7. go

    This go command causes the debugger to proceed until it reaches the return from the second constructor.

  8. go

    Another go causes the debugger to proceed until it reaches the return from the first constructor.

  9. go

    This go command causes the debugger to stop again at the return from the second constructor.


    After this go command, the Log window shows the output from eight print commands (4 from the entry to myfunc and 4 from the return from myfunc ). The values printed are 7, 14, 8, 16, 9, 18, 10, and 20. Also, notice that the debugger stops at the return from the destructor.

  10. go



    At each of these go commands, the debugger stops at the return from the destructor.

  11. break "X::Y" entry

    This command illustrates the warning message that is issued by the debugger when it cannot find a function in the debugger's list of visible functions. Because X::Y is not a valid function name for this program, a warning message appears in the Log window.

    See Functions in a Mix of C and C++ Code for more information about when you may see this warning message.

  12. go

    After this last go command, the execution completes, and the debugger terminates.

Chapter Contents



Top of Page

Copyright © 2001 by SAS Institute Inc., Cary, NC, USA. All rights reserved.