How to Use the Visual C++ DEBUGGER

来源:互联网 发布:mac打不开百度知道 编辑:程序博客网 时间:2024/06/04 18:32
Section FourHow to Use the Visual C++ DEBUGGERTable of ContentsPage1. When to Use the Debugger.................................. 12. Putting Developer Studio in DEBUG MODE.................... 23. How to Start the Debugger................................. 34. Setting and Using Breakpoints............................. 3Introduction............................................ 3Breakpoints............................................. 4Setting a LOCATION Breakpoint......................... 5The "quick-set" method.............................. 5The Breakpoint Dialog Box method.................... 6Setting a Breakpoint at a line.................... 6Set a Breakpoint on the "current line".......... 6Set a Breakpoint at a specific line of code..... 7Setting a Breakpoint at a specified function...... 7Removing a Breakpoint................................. 8The "Quick-remove" Method....................... 8The Breakpoint Dialog Box Method................ 8Setting a DATA Breakpoint............................. 9Setting a VALUE-CHANGE Breakpoint................... 10Setting a Breakpoint on an Array.................. 12Setting a Breakpoint on a STRUCTURE............... 13Setting a Breakpoint on a POINTER................. 15Setting a CONDITIONAL Breakpoint.................... 165. Debugging a Program....................................... 19Running (Starting) the Visual C++ Debugger.............. 19The "F5 Key Method.................................... 19The "Run-to-Cursor" Method............................ 20The "Step-Into" Method................................ 21Restarting and Exiting the Debugger..................... 21Step Through the Program One Line at a Time............. 22Run to Cursor......................................... 24Break Debugger Execution.............................. 26Examining the State of a Program........................ 26Using the VARIABLES Window............................ 27The LOCALS Tab...................................... 27The AUTO Tab........................................ 29The THIS Tab........................................ 30Using the WATCH Window................................ 30Finding a bug that causes an "Illegal Operation" error.. 34When to Use the Debugger.---------------------------------------------------------------------When your source-code compiles/builds with no ERRORS, you will wantto "run" (execute) the compiled (machine-code) form of your program(to see how to compile/build your program, see the document entitled" How to Create and Run a Simple Project").To "run" the executable form of your program, do thefollowing:- Click "Build" in the Main Menu [Main(6)].- Select the "Execute" option from the menu that appears and theprogram making up the current project will begin to run.If one of the following errors occur, you will need to use theDebugger to find the associated "run-time" error. Once the error isfound using the debugger, you must fix that error and compile-build-execute the program again.- The Program Output Window appears, but the program produces nooutput (assuming it is supposed to). All you see in the ProgramOutput Window is the message "Press any key to continue".- The Program Output Window appears and the program producesoutput, but either that output is incomplete and/or incorrect.The last line in the Program Output Window is "Press any key tocontinue".- The Program Output Window appears and the program produces someoutput, but either that output is incomplete and/or incorrect,AND the last line in the Program Output Window is NOT "Press anykey to continue". What has happened is that your program hasentered an INFINITE LOOP. To get out of an infinite loop, pressthe CTRL key and the 'c' key at the same time and then releaseboth at the same time. This will terminate the running programand put you back in the Editor.- The program begins to run, but then a dialog box having thesame name as your project appears displaying the error message:"The program has performed an illegal operationand will be shut down".This error message can appear for many and varied reasons (suchas division-by-zero, de-referencing a pointer that does notstore a legitimate address value, exceeding the bounds of anarray etc.This dialog box gives you three options. See the section belowentitled "Finding a bug that causes an ILLEGAL OPERATIONerror" to learn about each option.- If the program performs in some other strange manner as itexecutes.Putting Developer Studio in DEBUG MODE.-----------------------------------------------------------------------For the debugger to work properly, Developer Studio MUST be in "debugmode" when you compile a project. By default, the Visual C++ compilercompiles in debug mode. Therefore, unless you have changed thedefault state of the compiler, you do not need to do anything to havethe compiler create special files for use by the debugger.However, to be sure that the compiler is currently in "debug mode",do the following:1. Click "Project" in the Main Menu [MM(5)].2. Select the "Settings" option from the menu that appears and the"Project Settings" Dialog Box will appear.3. Click the "C/C++" tab at the top of the "Project Settings"dialog box. Be sure that the terms "Disable [Debug]" appear inthe "Optimizations" window. If not, then click the small "menuicon" immediately to the right of that window and select"Disable [Debug]".4. Now click the "Link" tab at the top of the Project SettingsDialog Box. Be sure there is a check mark in the "Generatedebug info" checkbox. If not, then click that checkbox to setit.How to Start the Debugger.----------------------------------------------------------------------1. Bring up the source-code file in the Visual C++ Editor.2. Be sure the compiler is in "debug mode". See "Putting DeveloperStudio in DEBUG MODE " above to do this.3. Be sure you have set "Breakpoints" where needed (see the section"Setting and Using Breakpoints" below to learn how to set upand make use of Breakpoints).4. Compile and build your program (if you have not already done so).5. Press the "F5" key to start the debugger. Your program will beginto execute, but will stop execution at the first Breakpoint. Tolearn what you can do when execution stops at a Breakpoint, seethe section below entitled "Setting and Using Breakpoints".Setting and Using Breakpoints.--------------------------------------------------------------------A. Introduction:-------------A BREAKPOINT is a condition which will suspend the execution ofyour program as it is executing under the Visual C++ Debugger.When program execution is stooped, the debugger will allow you toperform certain actions that will help you find a "run-time"(logical) program error (bug). Two of the most important actionsthe debugger will allow you to perform upon the occurrence of aBreakpoint include:1. You can "step" through execution of the program one (ormore) line(s) at a time. This stepping through a programallows you to see what actions, outcomes, events etc. areoccurring in your executing program line-by-line. This makesit easy to immediately detect when something goes wrong suchas when an incorrect value is assigned to a variable, or theindex of an array has exceeded acceptable bounds, or apointer has been incorrectly set, etc.2. You can have displayed in a debugger window the currentvalue of:- All visible LOCAL and GLOBAL variables;- A specified subset of visible LOCAL and GLOBALvariables.- All variables referenced in the line of code currentlybeing executed.See "Debugging a Program" below to see how do performthese actions.B. Breakpoints:------------The Visual C++ Debugger provides you with two kinds ofBreakpoints. A variety of options are available for each of thesetwo kinds of Breakpoints.1. LOCATION Breakpoints.Here the Breakpoint is associated with a specific line ofthe program. When the line of code for which the LocationBreakpoint is set is to be executed, program execution stopsand you can then use the debugger facilities to examine thecurrent state of the executing program to find a bug.2. DATA Breakpoint.Here the Breakpoint is associated with a specific data itemsuch as a variable. You can set the Data Breakpoint so thatprogram execution will stop when the value stored in thisspecified variable changes (i.e. is altered) or you can setthe Data Breakpoint to occur when the specified variable isassigned a certain value. When program execution stopsbecause of this Data Breakpoint, you can then use thedebugger facilities to examine the current state of theexecuting program to find a bug.Setting Breakpoints.WARNING - - When running your executable program under thedebugger, you may have to wait for prolongedperiods of time for the debugger to get to thefirst or next Breakpoint. This is particularlytrue if you have many "output" statements in yourprogram (the moral is not to have a lot of outputstatements in the form of the program you aretrying to debug). Comment out all but the mostessential output statements while debugging. Bepatient and just wait. If nothing seems to behappening after 5 minutes, turn off the debuggerand alter one or more of your Breakpoints and/orcomment out more output statements.1. Setting a LOCATION Breakpoint.------------------------------The following methods for setting/removing/disablingLocation Breakpoints will work if done from the Editor andwill also work if done while the program is being executedunder the debugger):A. The "quick-set" method.-----------------------To quickly set a Breakpoint on a specific line of thesource-code, do the following:a. Get the file that contains the line where theBreakpoint is to be set into the Editor Window. Placethe "Insert Cursor" anywhere on the line of code onwhich you want to set a Line Number Breakpoint andthen click the RIGHT mouse button.b. Select the "Insert/Remove Breakpoint" option fromthe menu that appears. A special "icon" will appearto the left of this line of code.To quickly remove a Breakpoint on a specific line of thesource-code, do the following:a. Place the "Insert Cursor" anywhere on the line ofcode on which you want to remove a Breakpoint and thenclick the RIGHT mouse button.b. Select the "Remove Breakpoint" option fromthe menu that appears.If you just want to temporarily DISABLE the Line NumberBreakpoint (not remove it), do the following:a. Place the "Insert Cursor" anywhere on the line ofcode which contains the Breakpoint to be disabled andthen click the RIGHT mouse button.b. Select the "Disable Breakpoint" option from the menuthat appears. When disabled, a Breakpoint will have noeffect.To enable a disabled Breakpoint, do the following:a. Place the "Insert Cursor" anywhere on the line ofcode which contains the Breakpoint to be enabled andthen click the RIGHT mouse button.b. Select the "Enable Breakpoint" option from the menuthat appears.B. The Breakpoint Dialog Box method.---------------------------------Using the Breakpoint Dialog Box to set a Line NumberBreakpoint at a specific line of source-code is not aseasy as method "A" above, but gives you more options tochoose from (i.e. is more powerful).To have the Breakpoint Dialog Box displayed, do thefollowing:1. Click "Edit" in the Main Menu [MM(2)].2. Select the "Breakpoints" option from the menu thatappears. The Breakpoint Dialog Box will appear.Note that there are three tabs at the top of theBreakpoint Dialog Box. Click the "Location" tab (if it isnot already selected) to set a Location Breakpoint. Theother two tabs are discussed in later sections.You can use the Breakpoint Dialog Box to set a LocationBreakpoint at a specific line number in the source code,or at the first line of a specified function.a. Setting a Breakpoint at a line.-------------------------------i. Set a Breakpoint on the "current line".---------------------------------------If you want to set a Breakpoint on the line wherethe "current insert position" has been set, do thefollowing (it is assumed that you have set the"current insert position" appropriately - rememberthe insert position is at the blinking verticalcursor)a. Click the "menu icon" at the immediate rightof the "Break At" text field in the dialogbox and a menu will appear.b. Select the option that begins with the word"Line" and is followed by an integer value.c. Click the "OK" button to set the Breakpointand exit the Breakpoint Dialog Box.ii. Set a Breakpoint at a specific line of code.--------------------------------------------a. Type a period and then the source-code linenumber into the "Break At" text field of thedialog box.b. Click the "OK" button on the dialog box. A small"stop sign" will appear to the left of the lineof code for which the Breakpoint was set.If you want to set several Line NumberBreakpoints consecutively, do the following:1. Type a period and then the line numberinto the "Break At" text field.2. Press the ENTER key.3. Repeat steps "1" and "2" above until youhave set all the Breakpoints you desire.4. Click the "OK" button on the dialog box.A small "stop sign" will appear to theleft of each line of code for which theBreakpoints were set.b. Setting a Breakpoint at a specified function.---------------------------------------------It is assumed that the Breakpoints Dialog Box iscurrently displayed.To set a Breakpoint at the first line of a specificfunction (external or member function), do thefollowing:1. Type the name of the function into the"Break At" text field. If the function is aMEMBER of a class, you must precede the memberfunction name with its "class" and "::".For example, if member function "doit" isdeclared in a class named "classa", then youwould have to type "classa::doit". Note thatyou do NOT type any parentheses or parameterlist after the function name.2. Click the "OK" button on the dialog box.If you want to set several Function Breakpointsconsecutively, do the following:1. Type the function name in the "Break At" textfield.2. Press the ENTER key.3. Repeat steps "1" and "2" immediately above untilyou have set all the Breakpoints you desire.4. Click the "OK" button on the dialog box to setthe Breakpoints and exit the Breakpoints DialogBox.2. Removing a Breakpoint.----------------------To remove any (or all) LOCATION Breakpoint(s) (Linenumber or Function Breakpoint), do the following:a. The "quick remove" method.--------------------------To remove a Breakpoint "set" using the "quick-set"method of creating a Breakpoint (as describedabove).1. Get the source-code that contains the Breakpointto be removed into the Editor Window.2. You will see a small "stop sign" immediately tothe left of the line that contains theBreakpoint.3. Put the "Insert Cursor" anywhere on that lineand click the RIGHT mouse button. A menu willappear.4. Select the "Remove Breakpoint" option and theBreakpoint is removed (and the small "stop sign"disappears).b. Using the Breakpoint Dialog Box to remove one ormore LOCATION (Line Number or Function)Breakpoints.1. Click "Edit" in the Main Menu [MM(2)].2. Select the "Breakpoints" option from the menuthat appears. The Breakpoint Dialog Box willappear.3. At the bottom of the Breakpoint Dialog Box youwill see all currently set Breakpoints. Clickthe name of the Breakpoint you want to removeand that name is highlighted,orif you want to remove ALL currently setBreakpoints, just click the "Remove All" buttonin the dialog box and then click the "OK" buttonin the dialog box to exit the Breakpoint DialogBox (and skip step "4" below).To temporarily DISABLE a Breakpoint listed atthe bottom of the Breakpoints Dialog Box, clickthe small "check" icon to the left of theBreakpoint name. To re-enable that Breakpoint,just click that same icon (and the "check" willreappear).4. Click the "Remove" button in the dialog box.To remove another Breakpoint, just repeat steps"3" and "4".Once you have removed all the Breakpoints youdesire, click the "OK" button in the dialog boxto exit the Breakpoint Dialog Box.Keep in mind that if you edit the source-code using someother editor (not the Visual C++ Editor), then yourBreakpoints can be all messed up. The moral is - editthe source-code only with the Visual C++ Editor.3. Setting a DATA Breakpoint.--------------------------The following methods for setting/removing/disabling DataBreakpoints will work if done from the Editor and will alsowork if done while the program is being executed under thedebugger):You can set a Breakpoint that will be activated when:a. The value of a variable changes during programexecution under the debugger (called a VALUE-CHANGEBreakpoint).b. The value of a specified conditional expressionbecomes true as the program executes under thedebugger (called a CONDITIONAL Breakpoint).Note that when you run the debugger with Data Breakpointsset, program execution will stop at the next Data Breakpointand a pop-up message window will appear with the message"Break ...... when" and an "OK" button. You must click the"OK" button to actually get to the next Data Breakpoint.To set a Data Breakpoint, you must use the "BreakpointDialog Box". To get this dialog box to be displayed, do thefollowing.1. Click "Edit" in the Main Menu [MM(2].2. Select the "Breakpoints" option from the menu thatappears. The Breakpoint Dialog Box will appear.Note that there are three tabs at the top of the BreakpointDialog Box. Click the "Data" tab to set a Data Breakpoint.Be very careful that you do NOT click the "Location" tab asyou will get no error when you try to set a "Data"Breakpoint with the "Location" tab, but the "Data"Breakpoint will not be set and will not work.A. Setting a VALUE-CHANGE Breakpoint.----------------------------------It is assumed you are in the Breakpoints Dialog Box.Here, program execution under the debugger will stop thenext time the value in the specified variable ismodified. However, the manner in which you set this kindof "Data" Breakpoint depends on where in the program thespecified variable is declared.1. If the variable on which you are setting theBreakpoint is declared as a GLOBAL variable (outsideany function), then do the following:a. Type the name of the GLOBAL variable in the textfield named "Enter the Expression to beEvaluated".If you want to set this kind of Data Breakpointon one or more additional GLOBAL variables, thenput the cursor on the "dotted box" that is inthe "Breakpoints" window at the bottom of thedialog box and click the left mouse button. Typethe name of the next GLOBAL variable on whichyou want to set this kind of Breakpoint, andthen click the dotted box again. Keep enteringGLOBAL variable names and clicking the dottedbox until you have set all the "global"breakpoints you desire.b. Click on the "OK" button in the dialog box toactivate all the Breakpoints (Data-Change orConditional) you just set and to exit the"Breakpoints" Dialog Box.2. If the variable on which you are setting theBreakpoint is declared in an EXTERNAL function (afunction that is not a member function of any class),then do the following (this includes function "main"):a. Click the small "menu icon" immediately to theright of the text field named "Enter theExpression to be Evaluated".b. Click the word "Advanced" that appears and the"Advanced Breakpoints" Dialog Box appears.c. Type the name of the variable (declared in theEXTERNAL function) on which you are setting aBreakpoint in the text field named "Expression".d. In the text field named "Function", type thename of the EXTERNAL function where this dataitem is declared (e.g. "main" or "doit" {withoutthe quotes}).e. In the text field named "Source file", type thename of the source-code file that contains theEXTERNAL function entered in step "d" above. Forexample, if "doit" is defined in source-code file"main.cpp", then type "main.cpp" (without thequotes).f. Click the "OK" button in the Advanced BreakpointDialog Box and it will disappear and you will beback at the Breakpoints Dialog Box.If you want to set this kind of Data Breakpoint onadditional variables declared in an EXTERNALfunction, just repeat steps "a" through "f" above.If not, then click the "OK" button in the BreakpointDialog Box to exit this dialog box.3. If the variable on which you are setting theBreakpoint is declared in a MEMBER function of aclass, then do the following:a. Click the small "menu icon" immediately to theright of the text field named "Enter theExpression to be Evaluated".b. Click the word "Advanced" that appears and the"Advanced Breakpoint" Dialog Box appears.c. Type the name of the variable on which you aresetting a Breakpoint in the text field named"Expression".d. In the text field named "Function", type thename of the MEMBER function in which the variableis declared preceded by "classname::" where"classname" is the name of the class of whichthis function is a member. For example, if thedata member on which you are setting this kind ofBreakpoint is declared in function "domore" whichis a member of class "List", then type"List::domore" (without the quotes).e. In the text field named "Source file" type thename of the source-code file that contains theDEFINITION (implementation) of this MEMBERfunction. For example, if "domore" is in source-code file "list.cpp", then type "list.cpp"(without the quotes).f. Click the "OK" button in the Advanced BreakpointDialog Box and it will disappear and you will beback at the Breakpoints Dialog Box.If you want to set this kind of Data Breakpoint onadditional variables declared in a MEMBER function,just repeat steps "a" through "f" above. If not, thenclick the "OK" button in the Breakpoints Dialog Box toexit this dialog box.Some helpful hints:a. Setting a Breakpoint on an ARRAY:---------------------------------For the following discussion of "arrays", it isassumed that if you are in the "AdvancedBreakpoint Dialog Box" that you will type invalues in the "Function" and "Source file" textfields as described above for simple variables.1. To set a Data Breakpoint on an "array" so thatprogram execution will stop any time the valuestored in any element of the array is altered,do the following. Type in the "Enter theexpression to be evaluated" text field of theBreakpoints Dialog Box (or the "Expression"text field of the Advanced Dialog Box of theBreakpoints Dialog Box) the name of the array(and nothing else). For example, if the nameof the array is "vect", then just type "vect"(without the quotes).2. To set a Data Breakpoint on a single elementof an array so that program execution willstop only if the value in that specifiedelement is altered, do the following. Type inthe "Enter the expression to be evaluated"text field of the Breakpoints Dialog Box (orthe "Expression" text field of the AdvancedDialog Box of the Breakpoints Dialog Box) thename of the array followed by [elem_num] where"elem_num" is the index of the array elementyou want to set the Breakpoint on. Forexample, if the name of the array is "vect"and you want to set a Data Breakpoint onelement index "3" of that array, then justtype "vect[3]" (without the quotes).3. To set a Data Breakpoint on the first "n"elements of an array so that program executionwill stop only if the value in one of thefirst "n" elements is altered, do thefollowing.- Type in the "Enter the expression to beevaluated" text field of the BreakpointsDialog Box (or the "Expression" text fieldof the Advanced Dialog Box of theBreakpoints Dialog Box) the name of thearray (and nothing else). For example, ifthe name of the array is "vect", then justtype "vect" (without the quotes).- Now look for the text field named "Enter thenumber of elements to watch in an array"in the Breakpoints Dialog Box. Place thecursor in that window and click the leftmouse button. Now erase any number that iscurrently in that window and type the numberthat will be used for "n" (the first "n"elements of the array to be watched).- Then click the OK button in the BreakpointsDialog Box as usual.b. Setting a Breakpoint on a STRUCTURE:------------------------------------For the following discussion of "structures", itis assumed that if you are in the "AdvancedDialog Box" that you will type in values in the"Function" and "Source file" text fields asdescribed above for simple variables.1. To set a Data Breakpoint on a "structure" sothat program execution will stop whenever thevalue stored in any field of the structure isaltered, do the following. Type in the "Enterthe expression to be evaluated" text field ofthe Breakpoints Dialog Box (or the"Expression" text field of the Advanced DialogBox of the Breakpoints Dialog Box) the name ofthe structure (and nothing else). For example,if the name of the structure is "rec", thenjust type "rec" (without the quotes).One caution - if the structure contains an"array-of-characters" (string) field, thedebugger may display the debugger output inASSEMBLY language form rather than source-codeform when the "string" field is altered. Ifthis happens, click the "Break Out" button onthe DEBUGGER Button Bar [Debugger(7)] and thesource-code form of the program will be re-displayed with the current line set to theappropriate Data Breakpoint.If you ever get "stuck" in an ASSEMPLYlanguage display of the Debugger Window, youcan get back to the SOURCE-CODE display bydoing the following:- Click "Window" in the Main Menu [MM(8)].The "Window" menu will appear.- Select the "Windows" option from thatmenu and the "Windows Dialog Box" willappear.- If there is already a "box" around thename of the window that is displaying theASSEMBLY language form of the program,just click on the "Close Window(s)"button, else click on the name of thewindow that is displaying the ASSEMBLYlanguage form of the program, and thenclick on "Close Window(s)".2. To set a Data Breakpoint on a single field ofa structure so that program execution willstop only if the value in that specified fieldis altered, do the following. Type in the"Enter the expression to be evaluated" textfield of the Breakpoints Dialog Box (or the"Expression" text field of the Advanced DialogBox of the Breakpoints Dialog Box) the name ofthe structure followed by .fieldname where"fieldname" is the name of the field you wantto set the Breakpoint on.. For example, if thename of the structure is "rec" and you want toset a Data Breakpoint on the "age" field ofthat structure, then just type "rec.age"(without the quotes).One caution - if a Data Breakpoint is set onan "array-of-characters" (string) field, thedebugger may display the debugger output inASSEMBLY language form rather than source-codeform when the "string" field is altered. Ifthis happens, click the "Break Out" button onthe DEBUGGER Button Bar [Debugger(7)] and thesource-code form of the program will be re-displayed with the current line set to theappropriate Data Breakpoint.c. Setting a Breakpoint on a POINTER:----------------------------------You can set a Data Breakpoint to occur when the"address" stored in a pointer is altered. To setthis kind of Data Breakpoint, type in the "Enterthe expression to be evaluated" text field of theBreakpoints Dialog Box (or the "Expression" textfield of the Advanced Dialog Box of theBreakpoints Dialog Box) the name of the pointervariable. For example, if the pointer variable isnamed "ptr", then type "ptr" (without thequotes).It is NOT possible to set a Breakpoint to occurwhen the value "pointed to" by the pointer isaltered (thus, if the pointer variable is "ptr",you cannot set a Breakpoint on "*ptr").You can get around this by assigning the valuepointed to another variable and then settinga Data Breakpoint on that other variable. Forexample, we could write:double *num_ptr=new double, tmp_num;and later in the program we could write:*num_ptr = val;tmp_num = *num_ptr;and then we can set a Data Breakpoint on"tmp_num".B. Setting a CONDITIONAL Breakpoint.---------------------------------It is assumed you are in the Breakpoints Dialog Box.Setting a CONDITIONAL Breakpoint will stop programexecution when a specified CONDITION arises. Here,program execution under the debugger will stop at theline of source-code where a specified expression(condition) first becomes TRUE. A condition can be anylegitimate C++ Boolean Expression. As soon as thatexpression evaluates to TRUE, program execution willstop. Example of BOOLEAN expressions include:v1 < v2, !done, k == 0, ((x!=y) && (!v))To set this kind of CONDITIONAL Breakpoint, do thefollowing:1. In the Editor Window, display the line of source-code that contains the BOOLEAN expression you aresetting the CONDITIONAL Breakpoint for.For example, if the condition on which aconditional Breakpoint is to be set is "k>=10",then you must get a line of source-code thatreferences this instance of "k" displayed in theEditor Window. For example, the line of code mightbe:ary[k]=a*b;When the BOOLEAN conditional Breakpoint is set onthat line of code, then program execution will stopwhen the value of "k" gets to be greater than orequal to "10" at this line of code.2. Place the "Insert Cursor" anywhere on the line ofcode displayed from doing step "1" above, and thenclick the left mouse button to select that line.3. Get the Breakpoints Dialog Box displayed by doingthe following:- Click "Edit" in the Main Menu [MM(2)].- Select the "Breakpoints" option from the menuthat appears. The Breakpoint Dialog Box willappear.4. Click the "Location" tab at the top of the"Breakpoints Dialog Box".5. Click the "menu icon" (the small right-pointingarrow) at the far right of the "Break at" textfield.6. Click the "Line #" option from the menu (where "#'is an integer value that represents the number ofthe line in the source-code that you selected instep "2" above). For example, if the "InsertCursor" was on line 170 when you selected a line instep "2" above, then click the "Line 170" menuoption. You will now see at '.170' in the"Breakpoints" window at the bottom of the dialogbox.7. Click the "Condition" button in the BreakpointsDialog Box and the "Breakpoint Condition" dialogbox will be displayed.8. In the "Enter the expression to be evaluated" textfield, type a C++ expression such asv1 < v2, !done, k == 0, ((x!=y) && (!v))For example, you might type:k>=10;9. Click the "OK" button in the "Breakpoint Condition"Dialog Box and that dialog box is removed. You willnow see:at '.170' when k>=10displayed in the "Breakpoints" window at the bottomof the Breakpoints Dialog Box.10. Click the "OK" button in the Breakpoints Dialog Boxto set this CONDITIONAL breakpoint and exit theBreakpoints Dialog Box.NOTE - setting "Data Breakpoints" is particularly usefulwhen trying to find a bug that is causing yourprogram to enter an INFINITE LOOP.Using the Breakpoint Dialog Box to remove one or more DATA(Value-Change or Conditional) Breakpoints.1. Click "Edit" in the Main Menu [MM(2)].2. Select the "Breakpoints" option from the menu thatappears. The Breakpoint Dialog Box will appear.3. At the bottom of the Breakpoint Dialog Box you willsee all currently set Breakpoints. Click the name ofthe Breakpoint you want to remove and that name ishighlighted,orif you want to remove ALL currently set Breakpoints,just click the "Remove All" button in the dialog boxand then click the "OK" button in the dialog box toexit the Breakpoint Dialog Box (and skip step "4"below).To temporarily DISABLE a Breakpoint listed at thebottom of the Breakpoints Dialog Box, click the small"check" icon to the left of the Breakpoint name. Tore-enable that Breakpoint, just click that same icon(and the "check" will reappear).4. Click the "Remove" button in the dialog box.To remove another Breakpoint, just repeat steps "3"and "4".Once you have removed all the Breakpoints you desire,click the "OK" button in the dialog box to exit theBreakpoint Dialog Box.Keep in mind that if you edit the source-code using someother editor (not the Visual C++ Editor), then yourBreakpoints can be all messed up. The moral is - editthe source-code only with the Visual C++ Editor.If you try to set a "bad" Breakpoint (a Location Breakpointon a line that does not exist, or a Data Breakpoint for avariable that does not exist or is not "visible") you willget an error message when you try to run your program in thedebugger. The error message will say "One or morebreakpoints cannot be set and have been disabled". If thishappens to you, click the OK button on the error messagebox, exit the debugger, and get back in the BreakpointsDialog Box and remove the "bad" Breakpoint (and set theproper breakpoint).Debugging a Program.--------------------------------------------------------------------Breakpoints have their effect (suspending program execution) onlywhen your program is run under the control of the Debugger. TheDebugger can only help you find RUN-TIME (logical) errors, notCOMPILE-TIME (syntax) errors. You cannot execute your program underthe Debugger's control until that program compiles without error.Note - your program will compile if only WARNINGS are reported by thecompiler. However, if the compiler reports even one (fatal)ERROR, then the program is not compiled and cannot be executedunder the Debugger. Get rid of all SYNTAX (compile-time)errors and then your program can be run under the Debugger.A. Running (Starting) the Visual C++ Debugger.-------------------------------------------There are three methods by which you can have the Debugger executethe program under its control.1. The "F5" key method.--------------------To run the Debugger using this method, do the following:- Get the project file that contains the function named"main" displayed in the Editor Window. It is assumed thatthe program was created as a PROJECT. To get this filedisplayed in the Editor Window, see the documententitled "Editing Source-Code Files".- Have the Debugger Button Bar displayed. To do this, do thefollowing:a. Place the mouse cursor over an empty area of the MainMenu (at the top of the screen).b. Click the RIGHT mouse button.c. Select the "Debug" option from the menu that appears.You can then move and/or dock this Debugger Button Barwhere you wish. It is usually best to "dock" theDebugger Button Bar.- Press the "F5" key on the keyboard. The debugger willstart executing the program that is the current projectand will suspend program execution at the first Breakpointit encounters (or when your program tries to perform an"Illegal Operation"). You will see some buttons on theDebugger Button Bar displayed "sharper" (darker) thanother buttons on that bar. The darker buttons are thosethat can be used (clicked) and the fainter (lighter)buttons are those that cannot at this time be used(clicked).For a "Location" Breakpoint, program execution stops onthe line of source-code where the Location Breakpoint wasset, and this line of source-code (and several lines aboveand below this line) is displayed in the Editor Window.If the Debugger suspends program execution due to theoccurrence of a "Data" Breakpoint, it displays a smallwindow that contains the message:Stop at ... condition ...and an "OK" button. Click the "OK" button and the sectionof your program source-code that contains the current DataBreakpoint is displayed in the Editor Window.You can now examine properties of your program and view thevalues stored in the variables currently "visible" by takingany actions described under the heading "Examining the State ofYour Program" below.To re-start the Debugger when a Breakpoint is encountered, andhave it continue executing your program until the nextBreakpoint is encountered (or until the program ends), justpress the "F5" key again.To terminate and exit the debugger, click the "Stop Debugging"button on the Debugger Button Bar [Debug(2)]. You will now seemost of the buttons on the Debugger Button Bar become very"faint" (light) which means they are inactive and cannot beused. Some of the buttons will remain "sharp" (dark) whichmeans they are still active2. The "run-to-cursor" method.---------------------------With this method, you can execute your program until it reachesthe line of source-code where you placed the "Insert Cursor".To have the Debugger start program execution and then suspendexecution at the current "Insert Position", do the following:- Get the line of source-code on which you want to set thiskind of temporary Breakpoint displayed in the EditorWindow.- In the Editor Window, place the "Insert Cursor" on theline of source-code where the temporary Breakpoint is tobe set and then click the left mouse button to "set" the"Insert Position" (and select this line).- Click the "Run to Cursor" button on the Debugger ButtonBar [Debug(8)] (you do not have to press the F5 key) andyour program will be executed until the line of source-code on which the current "Insert Position" was set isreached. At that point, program execution is suspended andnow you can examine properties of your program and viewthe values stored in the variables currently "visible" bytaking any actions described under the heading "Examiningthe State of Your Program" below.You can set the "Insert Position" for any line of any filethat makes up your program. Remember, this kind of"temporary" Breakpoint is NOT maintained from onedebugging session to another.3. The "step-into" method.-----------------------Another way to "start" the debugger is to click the "Step Into"button on the Debugger Button Bar [Debug(5)] (or the "StepOver" button [Debug(6)]). Clicking either of these two buttonshas the same effect; the debugger will start, but will suspendprogram execution at the first line of function "main". You canthen examine properties of your program and view the valuesstored in the variables currently "visible" by taking anyactions described under the heading "Examining the State ofYour Program" below.B. Restarting and exiting the debugger.------------------------------------1. The Debugger MUST already be running your program before youcan "re-start" Debugger execution (i.e. do not try to re-startthe Debugger when it is not currently running your program).To RE-START the debugger means that the "current debuggingline" is re-set from its current position in the program codeto the first line in function "main". To RE-START the debugger,click the "Restart" button on the Debugger Button Bar[Debug(1)], and then press the "F5" key on the keyboard. Whenyou press this key you will note that the debugger startsrunning the program from the first line of function "main" andsuspends execution at the first Breakpoint it encounters (whichis not necessarily the same Breakpoint where you were when youinitiated the re-start action).The purpose of "re-starting" the debugger is to have thedebugger re-run your program from its beginning so you can (a)review the execution of lines of code you have already passedthrough, or (b) to re-run the program after you have made achanges to variable values or program code and you want to seethe effects of these changes. That is, you can "edit" yoursource-code while in the debugger and then re-start thedebugger to see what occurs as a result of the editing.Note - if you make a change to your source-code (i.e. edit thesource-code) while you are in the debugger and then try to "re-start" the debugger, you will get the message "One or morefiles are out of date or do not exist. Would you like to buildthem?" Click the "YES" button and your altered programs will bere-compiled and then the debugger will re-start the program.Remember to press the "F5" key each time you want to run thedebugger to the next Breakpoint.2. To stop and exit the debugger and return to the Editor, clickthe "Stop Debugging" button on the Debugger Button Bar[Debug(2)]. If you choose this option, you cannot "re-start"the debugger by clicking the "Restart" button. You must use oneof the three "start debugger" methods described above to startthe debugger running again.C. Step through the program one line at a time.--------------------------------------------You may want to execute certain lines of the program one line at atime. You could then:a. Examine the value of one or more variables after the currentline of code is executed.b. See which path through a conditional statement is taken.c. View the output produced by the execution of a line of code.d. Check to see if a function is entered.Executing a program (under the control of the debugger) one lineof code at a time is called "stepping through a program".Once a Breakpoint has been encountered and the debugger hassuspended program execution, you can "step through" one or morelines of code (and have that code executed) by taking one of thefollowing three actions.1. Click the "Step Over" button on the Debugger Button Bar[Debug(6)]. When this button is clicked, the line of codepreceded by the "current line marker" (which is a smallright-pointing arrow at the left of the line of code), isexecuted and then the "current line marker" is moved to thenext line of code that would be executed if this button wereto be clicked again. The next line of code to be executed isnot necessarily the line immediately below the line of codejust executed. Which line is executed next depends upon theflow of program control. Each time you click the "Step Over"button, the line preceded by the "current line marker" isexecuted.Note that if the next line of code to be executed is a callto a function, clicking the "Step Over" button will "stepover" (i.e. not enter) that function. However, all the codecontained within that function will automatically beexecuted (even if it contains many lines of code). Thus, ifthis "stepped-over" function changes the values of variablesand/or produces outcomes, you can be sure that all thesechanges and outcomes will have taken place and will havetheir effect on subsequent program execution. That is tosay, "stepping over" a function does NOT result in the codecontained in that function being left unexecuted. The codein that function will be executed as one step. After thecode in the skipped function has been executed, the "currentline marker" will be moved to the first line of executablecode that follows the line where that function was called.Click the "Step Over" button to avoid entering functions inwhich you have no interest (i.e. you do not want to stepthrough the code in that function). Remember, many calls tofunctions do not look like a call to a function. Forexample:cout << str << endl;is a call to a built-in overloaded operator function. If youclick the "Step Over" button when the above line of code isthe "next-to-be-executed" line, then the code contained inthat function will be executed, but you will not "step into"than function (which you probably would not want to doanyway).If you are not sure which of the three "step" buttons onthe Debugger Button Bar to click, it is always safest toclick the "Step Over" button.2. Click the "Step Out" button on the Debugger Button Bar[Debug(7)]. When this button is clicked, the debuggerautomatically executes the remaining unexecuted lines ofcode in the current function, exits that function, and stopsprogram execution at the next line of code to be executedfollowing the call to that function. Thus, this provides away to leave the currently executing function and, thereby,make it unnecessary to "step through" the remainder of thecurrent function. Use the "Step Out" option if:a. You want to exit the function you are currentlystepping through because you have no need to stepthrough any more of its code, or;b. You accidentally "stepped into" a function you do notwant to step through and you want to immediately getout of that function.3. Click the "Step Into" button on the Debugger Button Bar[Debug(5)]. This option works just like the "Step Over"button except that if the next line of code to be executedis a call to a function, then when the "Step Into" button isclicked, the function that is called WILL be entered (andnot skipped as with the "Skip Over" option), and the nextline of code executed by the debugger will be the first lineof code in the function entered.Be careful about clicking "Step Into" because it is veryeasy to "step into" uninteresting functions accidentally.For example, if the next line of code to be executed when"Step Into" is clicked is a "built-in" function such as"strcpy", then the debugger will actually enter thatfunction and stop at the first line of code in that function(and you probably have no interest in looking at theexecution of code within that function).When the debugger enters certain "built-in" functions (e.g.strcpy), it displays the code in that function in ASSEMBLYlanguage form (which may not be of much use to you). Inother cases, the debugger will display the built-in functionin its source-code from. For example, if the next line ofcode to be executed when you click the "Step Into" buttonis:cout << str << endl;then the debugger will enter this overloaded operatorfunction for the stream insertion operator for a stringvalue (<<) and the code contained in that function isdisplayed in its source-code form which you will have toeither step through or "step out" of.If you accidentally "step into" a function you do not wantto step through, just click the "Step Out" button [Debug(7)](as discussed above), and the debugger will make the line ofcode that follows the call to this function the next-to-be-executed line.The moral is WATCH WHAT YOU STEP INTO.There are two other interesting buttons on the Debugger Button Barthat you may need to use:Run to cursor.--------------1. Click the "Run to Cursor" button on the Debugger Button Bar[Debug(8)] to:a. Start the debugger running and have the debugger suspendprogram execution at a specified line of code. To specifythe line of code at which program execution should besuspended, do the following (it is assumed that yoursource-code is in the "Editor Window" and the debugger isnot currently running):- In the Editor Window, place the "Insert Cursor" onthe line where you want program executionsuspension to occur.- Click the left muse button to "select" that line.Now if you click the "Run to Cursor" button, the debuggerwill start and will execute your program until it reachesthe "selected" line of code at which point it willsuspend program execution. You can then choose one of theother debugger options. Keep in mind that if a regularBreakpoint is encountered before the selected line isreached, program suspension will occur at that regularBreakpoint and the "run to cursor" setting will be lost.You can think of this method as setting a "temporary"Breakpoint which will disappear the first time it isencountered.b. Have the debugger continue program execution from thecurrent Breakpoint until a specified line is reached atwhich point program execution is suspended again. Here itis assumed the debugger is already running, but hasstopped at a Breakpoint.To continue program execution to the specified line, dothe following:- In the Editor Window, place the "Insert Cursor" onthe line where you want program executionsuspension to occur. This line of code must be onethat FOLLOWS (is executed after) the currentdebugging line.- Click the left muse button to "select" that line.Now if you click the "Run to Cursor" button, the debuggerwill CONTINUE executing your program until it reachesthe "selected" line of code at which point it willsuspend program execution. You can then choose one of theother debugger options. Keep in mind that if a regularBreakpoint is encountered before the selected line isreached, program suspension will occur at that regularBreakpoint and the "run to cursor" setting will be lost.The "Run-to-Cursor" option is great for getting out ofexecuting loops. Just put the cursor on the first line ofcode below the loop statement, click the left mousebutton to select that line, and then click the "Run toCursor" button. The loop will complete looping in itsnormal manner and program execution will be suspended atthe first line of code below that loop.Break Debugger execution.-------------------------2. Click the "Break Execution" button on the Debugger ButtonBar [Debug(3)] if the debugger is currently actively runningyour program (that is it is actively executing lines of codeand is not stopped at a Breakpoint), and you want to havethe debugger stop executing lines of program code (i.e.suspend itself). You may want to "break execution" so youcan exit the debugger, or perform other debugger actions.Clicking "Break Execution" will not cause you to exit thedebugger, it will just stop the debugger from executinglines of program code. If the debugger is not activelyexecuting program code, clicking the "Break Execution"button will have no effect.D. Examining the state of a program.---------------------------------Before trying to do any of the following, bring up the "DebuggerButton Bar" first by doing the following:a. Place the mouse cursor over an empty area of the Main Menu(at the top of the screen).b. Click the RIGHT mouse button.c. Select the "Debug" option from the menu that appears. Youcan then move and/or dock this Debugger Button Bar where youwish. It is usually best to "dock" the Debugger Button Bar.When the debugger has suspended program execution because aBreakpoint has been encountered, you can take any of the followingactions to examine program properties and to view the valuesstored in currently visible variables/parameters of your program.To debug a program it is often necessary to examine the valuestored in a specific variable (or set of variables) at specificpoints during program execution. You may want to determine thevalue assigned to a counter, an array index specifier, a functionparameter, or just some variable as the program is executed underthe control of the debugger. You may want to see what main memoryaddress is currently stored in a "pointer", or you may want toexamine the value(s) stored in the block of memory pointed to by apointer. You can see what values are stored in the fields of astructure and/or the data members of an object. It is easy toexamine and trace the values assigned to program variables as theprogram executes under the control of the debugger.1. Using the VARIABLES window.---------------------------a. The LOCALS Tab.--------------You can examine and trace the value currently assigned toeach variable/parameter that is currently visible in thefunction being executed by the debugger. You CANNOT examinevariables that are not currently visible (e.g. as variablesthat are local to a function that is not the currentlyexecuting function). CURRENTLY VISIBLE means:- All parameters passed into the currently executingfunction.- All variables declared within the currently executingfunction (i.e. all LOCAL variables).- All GLOBAL variables that do not share the same nameas a currently visible LOCAL variable or parameter ofthe currently executing function.In addition, if the value associated with any one of the"visible" variables/parameters is altered by code in theexecuting function, you will see the new (altered) valueassigned to the variable/parameter at the instant that valuechange occurs. In this way, you can see how variable valueschange as the program executes and where in the program avariable is assigned an incorrect value (that is, where aprogram error has occurred).Once you know where such a misasignment of a valueoccurred, you can examine your code to see why this erroroccurred and fix that error.To have the value of EVERY visible variable/parameterdisplayed in a special window, do the following:- Click the "Variables" button on the Debugger Button Bar[Debug(11)].- At the bottom of the special window that appears youwill see three tabs. Click the "Locals" tab (if it isnot already selected).You will now see two columns; one called "Name" and onecalled "Value". The data item(s) under "Name" is the name ofa currently visible variable/parameter, and the data itemunder "Value" is the value associated with the variablewhose name is listed to the immediate left.For simple data types such as "int", "float", "double", and"char", you will see the variable name and its associatedvalue. If a visible variable/parameter has not been assigned(or initialized to) a value as yet, you will see somestrange value reported (or an "error message" is displayed).For more complex data types such as an array, a structure, apointer, or an object, the "Name" data item is preceded by asmall plus (+) icon. Click this icon to view the separatevalues stored within this more complex variable (note thatthe '+' icon becomes a '-' icon). For example:Click the '+' preceding And see----------------------- -------------------------------An array The values stored in eachELEMENT of the array.A structure The values stored in each FIELDof the structureA pointer The value(s) stored in theblock of memory pointed to bythe pointer.An object The values stored in each datamember of the object.Use the vertical scroll bar at the right side of thisspecial "Variables" window to scroll up and down in the listof variable names/values.To get back to only the variable name with a '+' icon to itsleft, just click the '-' icon to the left of the variablename.Note - the "Value" column entry can be moved to the right ifit is covering its associated "Name" column entry. Todo this, move the cursor over the vertical bar thatis just to the left of the 'V' of "Value". You willsee a special "move window" icon appear. Press andhold the left mouse button while you drag this iconleft or right and then release the left mouse button.One additional useful action you can perform while this"Variable" window is displayed is to manually alter thevalue associated with one or more of the displayed visiblevariables/parameters.For example, if you see something line:Name Valueprice 29.95you can change the value assigned to the variable/parameter"price" to 34.50 by doing the following:- Place the cursor to the right of the "Value" entry(29.95) for the variable "price".- Double click the left mouse button.- Press the "backspace" key on the keyboard to erase theold "value" associated with "price" (29.95).- Key in the new value for "price" (34.50).- Press the ENTER key to "set" the new value.From the current point in program execution, the value of"price" will be whatever you keyed in and will remain thatvalue until the program alters it at some later point (oryou alter "price" again by doing the same variable-changeaction as noted above).b. The AUTO Tab.-------------You may find that you do not want to see ALL currentlyvisible variables/parameters. You may want to see only thosevariables or parameters that are accessed on the "next-to-be-executed" line of code. For example, if the line of codethat is to be executed next is:res = a * b + val;you can choose to examine just the values currently storedin the variables "res", "a", "b", and "val" (and no othercurrently visible variables). To examine only the variablescurrently being referenced, click the "Auto" tab at thebottom of the "Variables" window. You will now see in the"Variables" window under the "Name" column only the names ofthose variables and parameters being referenced in the lineof code to be executed next. To the right of each "Name",you will see the value assigned to it. As with the "Locals"tab, you can alter the VALUE associated with any of thevariables or parameters listed under the "Name" column.To alter the "Value" entry of a variable, do the following:- Place the cursor to the right of the "Value" entry forthe variable.- Double click the left mouse button.- Press the "backspace" key on the keyboard to erase theold "value" associated with the variable.- Key in the new value.- Press the ENTER key to "set" the new value.From the current point in program execution, the value ofthat variable will be whatever you keyed in and will remainthat value until the program alters it at some later point(or you alter it again by doing the same variable-changeaction as noted above).In the same manner as described above, you can click the '+'icon to the left of arrays, structures, objects, pointers,etc. to view the values stored inside these more complexdata types.c. The THIS Tab.-------------If the debugger suspends program execution inside a MEMBERfunction of an object (due to a Breakpoint beingencountered), you can click on the "This" tab to view all ofthe data members of this object. In the special window willbe displayed the word "this" with a "+" icon to its left.Just click on the "+" icon and all the data members of thisobject and their current value will be displayed. To removethe data members (and have just the word "this" displayed),click the "-" icon that is to the left of the word "this".2. Using the WATCH window.-----------------------You can use the WATCH window to examine only those visiblevariables/parameters you desire. You can also use the WATCHwindow to set up as many as four distinct sets of variables andparameters to examine and trace.You can examine and trace the value currently assigned to eachdesignated variable/parameter in the function being executed bythe debugger. You CANNOT examine variables that are notcurrently visible (e.g. variables that are local to a functionthat is not the currently executing function). CURRENTLYVISIBLE means:1. All parameters passed into the currently executingfunction.2. All variables declared within the currently executingfunction (i.e. all LOCAL variables).3. All GLOBAL variables that do not share the same nameas a currently visible LOCAL variable or parameter of thecurrently executing function.In addition, if the value associated with any one of thedesignated variables is altered by code in the executingfunction, you will see the new (altered) value assigned to thevariable/parameter at the instant that value change occurs. Inthis way, you can see how variable values change as the programexecutes and where in the program a variable is assigned anincorrect value (that is, where a program error has occurred).Once you know where such a misalignment of a value occurred,you can examine your code to see why this error occurred andfix that error.a. To have the value of a specific visible variable/parameterdisplayed in a special window where ONE OR MORE variables orparameters have already been set to be watched, do thefollowing:- Click the "Watch" button on the Debugger Button Bar[Debug(10)].- At the bottom of the special window that appears you willsee four tabs named "Watch1" through "Watch4". Click thetab for the "Watch Window" to which you want to add thename of a variable or parameter.You will now see two columns; one called "Name" and onecalled "Value". Under "Name" you must enter the name of thecurrently visible variable/parameter you want to "watch".b. To have the value of a specific visible variable/parameterdisplayed in a special window where NO variables orparameters have been set to be watched, do the following:(Note - if no variable name has been as yet added to thistab's window, you will see no variable names under "Name"and no values under "Value". You will just see two emptyboxes.)- Place the cursor over the empty box (which may behighlighted or not) under the "Name" heading and click theleft mouse button once. That box will now display ablinking cursor.Note - under some conditions the "empty box" under the"Name" heading is not currently highlighted. Inthis case, place the mouse cursor over the non-highlighted box and click the left mouse buttononce (or if once is not enough, click a secondtime) and then you will see the blinking cursor.- Type the name of the currently visible variable orparameter that you want to "watch" and then press theENTER key. The current value of that variable/parameter isimmediately displayed in the "Value" column. If the valueassociated with this variable or parameter subsequentlychanges, that change will be immediately displayed in the"Value" entry box.Note that if you enter the name of a non-existent or non-visible variable or parameter, an error message isdisplayed.For simple data types such as "int", "float", "double", and"char", you will see the variable name and its associatedvalue. If a visible variable/parameter has not been assigned(or initialized to) a value as yet, you will see some strangevalue reported (or an "error message" is displayed).For more complex data types such as an array, a structure, apointer, or an object, the "Name" data item is preceded by asmall plus (+) icon. Click this icon to view the separatevalues stored within this more complex variable (note that the'+' icon changes to a '-' icon). For example:Click the '+' preceding And see----------------------- ---------------------------------An array The values stored in each ELEMENTof the array.A structure The values stored in each FIELD ofthe structureA pointer The value stored in the block ofmemory pointed to by the pointer.An object The values stored in each datamember of the object.If the name of the variable/parameter you enter is too wide forthe entry box, it is automatically scrolled horizontally asyou type.Note - the "Value" column entry can be moved to the right if itis covering its associated "Name" column entry. To dothis, move the cursor over the vertical bar that is justto the left of the 'V' of "Value". You will see aspecial "move window" icon appear. Press and hold theleft mouse button while you drag this icon left or rightand then release the left mouse button.Use the vertical scroll bar at the right side of this special"Variables" window to scroll up and down in the list ofvariable names/values if necessary.To get back to only the variable name with a '+' icon to itsleft, just click the '-' icon to the left of the variable name.One additional useful action you can perform while this"Variable" window is displayed is to manually alter the valueassociated with one or more of the displayed visible variables.For example, if you see something like:Name Valueprice 29.95you can change the value assigned to the variable/parameter"price" to 34.50 by doing the following:- Place the cursor to the right of the "Value" entry for thevariable "price".- Double click the left mouse button.- Press the "backspace" key on the keyboard to erase the old"value" associated with "price" (29.95).- Key in the new value for "price" (34.50).- Press the ENTER key to "set" the new value.From the current point in program execution, the value of"price" will be whatever you keyed in and will remain thatvalue until the program alters it at some later point (or youalter "price" again by doing the same variable-change actionas noted above).You can add as many VISIBLE variable and/or parameter namesunder the "Name" column as you desire. Just repeat the "add"operation as described above.You can create a different set of variables/parameters to be"watched" in each of the four different Watch Windows. Justclick on the tab (of the four available tabs) for theappropriate Watch Window and then add as many variables orparameters to that Watch Window as you want to "watch".To remove a variable or parameter from a "Watch Window"(which means that it will no longer be "watched"), do thefollowing:- Click on the "tab" for the Watch Window you want toremove a variable or parameter from.- Place the cursor to the right of the name (in the "Name"entry column) of the variable or parameter to beremoved).- Double click the left mouse button and you will see ablinking cursor.- Press the "backspace" key on the keyboard to erase thevariable/parameter name.- Press the ENTER key and this variable/parameter isremoved from the Watch Window and will no longer be"watched".E. Finding a bug that causes an "Illegal Operation" error.-------------------------------------------------------Often when you run a program you will get a "This program tried toperform an illegal operation ..." error message which states thatyour program has performed an "illegal operation". This "run-time"error is usually caused by your program trying to access the value"pointed-to" by a pointer, but the pointer variable eithercurrently stores "NULL" or it stores "garbage bits" (because yourprogram never assigned that pointer any legitimate address).You will get this message for many other kinds of "run-time"errors (such as divide-by-zero, etc.).To find the line of code that caused the "illegal operation", dothe following as soon as the "Illegal Operation" message isdisplayed on the screen.1. Click the CLOSE button on the window that displays the"illegal operation" message to close this window.2. The window where your program output is shown is nowdisplayed on the screen with the instruction to "Press anykey to continue". Press the spacebar key.3. Press the F5 key to start the debugger.4. A window with the message "Unhandled Exception" will appear.Click the OK button in this window to remove it.5. You will now see the program source-code in the EditorWindow and a small right-pointing arrow may be to the leftof the line of code that caused the "illegal operation".Unfortunately, some run-time errors (such as divide-by-zero)do not produce a right-pointing arrow on the line of codethat caused the error. If this occurs, then immediatelyclick the "Stop Debugging" button on the Debugger Button Bar[Debug(2)] to get out of the debugger, skip steps 6 and 7below and, instead, do the following:- Start execution of your program again.- When the "This program tried to perform an illegaloperation" message appears, click the "Details" buttonin the error message window and you may (or may not)get an explanation of the error that occurred. However,you probably will get no indication as to WHERE in theprogram (what line) the error occurred. You must gosearch for the error.- After reading the explanation of the error, click the"Close" button in the error message window.- The window where your program output is shown is nowdisplayed on the screen with the instruction to "Pressany key to continue". Press the spacebar key.Now you are back in the Editor Window and you can gosearch for the error and fix it.Skip steps 6 and 7 below.6. Click the "Variables" button on the Debugger Button Bar[Debug(11)] and the Variables Window will appear.7. Now click the AUTO tab to examine the value stored in thevariables referenced in the "current" line of code(especially any "pointer" variables) to determine if any ofthem store an incorrect value (for the pointer variable(s)an incorrect value would be zero (NULL), or some erroneousaddress such as some negative integer value, or a very smallpositive integer value).Note - to make life much easier for yourself when you mustdebug a program that references pointers, you shouldALWAYS initialize all pointer variables to NULL whenyou create (declare) them. For example, write:double *num_ptr = NULL; //Now num_ptr is zeroand not:double *num_ptr; //Now num_ptr contains garbageThis way, when you examine a pointer variable to seewhat value it stores, you will either see a truememory address or zero (NULL) assigned to it (and youwill not have to try to figure out whether theaddress stored in a pointer is some kind of "badaddress" resulting from "garbage bits" being storedin the pointer variable).Once you have found the variable that stores the "badvalue", click the "Stop Debugging" button on the DebuggerButton Bar [Debug(2)] to get out of the debugger and be putback into the Editor. You can now "fix" this run-time errorand recompile/build your program and try running it again.

 

原创粉丝点击