Understanding Control Flow and Debugging in C++

e81 cse 428s n.w
1 / 9
Embed
Share

Explore how control flow influences program state modification in C++, learn the importance of debugging to identify and fix program errors, and discover effective debugging techniques for C++ programming. Also, delve into an example program demonstrating prefix addition expressions and ways a program can fail.

  • C++
  • Debugging
  • Control Flow
  • Programming

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. E81 CSE 428S: Multi-Paradigm Programming in C++ Control Flow and Debugging Department of Computer Science & Engineering Washington University, St. Louis MO Chris Gill cdgill@wustl.edu 1

  2. Control Flow Modifies State Statements in the code are reached and run Some of them (e.g., operators, etc.) modify state Logic determines which statements are run And the order in which they are executed Operator precedence complicates this too E.g., postfix vs. prefix increment/decrement Look at what has been (or will be) changed May need to trace execution, watch state values in a debugger like gdb (compile with g in g++) 2 CSE 428S Multi-Paradigm Programming in C++

  3. Why Debug a Program? When your program crashes Due to a segmentation fault or floating point error If it had permission to write a core file, you can examine program state (core memory) at that point When a bug occurs in your program Watching program variables Identifying where a problem occurs (and why) Tracing through a program s behaviors Learning what a program does Good basic testing approach 3 CSE 428S Multi-Paradigm Programming in C++

  4. How to Debug a Program A debugger helps us observe a program s behavior Step through (and into) functions Watching the call stack and variable values But, before we start using the fancy tools What do we expect the program to do? How might the program fail? Can we make predictions and test them? Thinking: the most powerful way to debug Scientific method should guide what you do hypothesis, prediction, experiment, analysis The tools help you follow this disciplined approach faster 4 CSE 428S Multi-Paradigm Programming in C++

  5. An Example Program Called with command line arguments ./studio3 + 8 + 9 10 Calculates prefix addition expressions + 8 + 9 10 + + 8 9 10 These are equivalent to their infix versions (8 + (9 + 10)) ((8 + 9) + 10) Key idea: traverse expression, calculate value 1 same result 1 + + 2 3 2 5 different order 8 + + 10 4 5 3 4 9 8 10 9 5 CSE 428S Multi-Paradigm Programming in C++

  6. How the Program can Fail Too few arguments in expression ./studio3 + 8 + 9 Cannot calculate result (needs another value to finish 2nd + operation) 1 + 2 3 8 + 4 ??? 9 Try this on your own in the studio, for practice 6 CSE 428S Multi-Paradigm Programming in C++

  7. Essential gdb Commands Debug a program from its start bash> gdb studio3 Set a breakpoint at a line or a function entry point (gdb) break parse_and_compute Start execution with command line parameters (gdb) run + + + + + + 1 2 4 8 16 32 64 Print out the value of an expression (gdb) print argv[current_index] Continue execution until the next breakpoint (gdb) continue Show the program s call stack (gdb) where Execute one line, stepping into any functions (gdb) step Execute one line, stepping over any functions (gdb) next 7 CSE 428S Multi-Paradigm Programming in C++

  8. Debugging with g++ and gdb in emacs Modify your Makefile (it passes g switch to g++) EXECUTABLE = studio3 CMPL_SRCS = studio3.cpp HEADER_FILES = studio3.h Compile the program via the make command g++ -o studio3 DUNIX -Wall -W -g -std=c++17 studio3.cpp Open the source and header files in emacs emacs studio3.cpp studio3.h Activate gdb within emacs (Esc x gdb) run gdb (like this): gdb -i=mi studio3 Use like in stand-alone gdb (but with tab-completion!) (gdb) break parse_and_compute(int&, int, char**) (gdb) run + + 9 8 + 7 + 6 (gbd) continue (gdb) where As you debug, emacs highlights the current code line! 8 CSE 428S Multi-Paradigm Programming in C++

  9. Studio 3 Practice using gdbto observe a program s behavior Step/run through the program s control flow Print out important expressions at key points Consider cases where program succeeds vs. fails Studios 0 through 11 are due 11:59pm Monday October 16th (the night before Exam 0) Submit as soon as each is done so you get feedback and can resubmit any that may be marked incomplete 9 CSE 428S Multi-Paradigm Programming in C++

More Related Content