Basic Errors Compiling in Unix Syntax errors Common Errors, and Debugging Run-Time errors CS-121 Logic errors Syntax Errors Syntax Errors An error in which a C++ grammar rule has been violated. Are flagged at compile time Missing ; Program cannot be converted to machine code until these are fixed Uneven quotes Easiest errors to find once you get used to Misspellings them Undefined variables
Syntax Errors emacs flags syntax errors in compilation mode Syntax Error Example ESC-X compile : to enter compilation mode you MUST have a Makefile Place your cursor on the error and hit enter you will jump to the appropriate place in the source file Run-Time Errors Run-Time Errors In Unix run-time errors create core-dumps Errors detected by the computer during program execution (program crashes) a file named “core” Common run-time errors contains run-time information division by zero The line-number where the error occurred accessing a memory cell you don’t have permission to access (well get to this later) The values of variables at the time the program crashed
Run-Time Errors Run-Time Errors In order to generate useful core-dumps your program must be compiled with debugging information In a Makefile you can enable debugging g++ helloworld.cpp -o helloworld -g information by setting the CPPFLAGS variable at the begging of the Makefile The -g option enables CPPFLAGS=-g debugging information Run-Time Errors Run-Time Errors gdb commands bt : (backtrace) list the functions that are Use gdb (GNU debugger) to see the core executing when the error occurred dump info There may be more than one each ESC-X gdb (in emacs) stored in a “frame” gdb ./hellworld core (on the shell) Tells you where in the program the error occured The machine code The core dump file (executable) file frame [num] : Select the frame [num]
Run-Time Errors gdb commands Run-Time Error Example display [var_name] : display the contents of the variable when the error occured quit : quits gdb Logic Errors Logic Errors An error that occurs when we have a faulty algorithm You also use gdb to trace through programs These are the hardest errors to find gdb ./helloworld Not flagged at compile time The program must be compiled with Not flagged at run-time debugging information Usually need to trace through the program (execute it line-by-line)
Logic Errors Logic Errors gdb commands gdb commands (for tracing) list : list the source code of the program break [filename]:[line number] : tells gdb to (use only in shell mode) stop the program written in file [filename] at line number [line number] and begin next : execute the next line tracing from that point cont : continue running the program (stop run : runs the program tracing) gdb trace example
Recommend
More recommend