C++ crash course

download C++ crash course

of 22

  • date post

  • Category


  • view

  • download


Embed Size (px)


C++ crash course. Class 9 flight times program, using gdb. Using GDB. Why use a debugger? Usually you can figure out what ’ s going on just by using cout (print statements) - PowerPoint PPT Presentation

Transcript of C++ crash course

  • C++ crash courseClass 9flight times program, using gdb

  • Using GDBWhy use a debugger?

    Usually you can figure out whats going on just by using cout (print statements)Debuggers allow for more fine-tuned control, and dont force you to repeatedly re-compile your code every time you want to test something new

  • Using GDBC++ programs with the g++ compiler can use GDB, or the Gnu debuggerFunction of a debugger:set breakpointsexamine the contents of variablesdereference pointers to see what theyre pointing tostep into functions to see whats happeningWhat does that all mean?

  • Using GDBFirst step: we need to compile the program especially for debugging purposes

    Add the flag g when compiling

    Your Flight Times programs:

    $ g++ -g -o flighttimes-dbg Main.cpp FlightTime.cpp TravelAgent.cpp

    This will create a flighttimes-dbg executable

  • Using GDBWithout the debugger, that executable will run perfectly normally

    can do: ./flighttimes-dbg < sampleinput

    ...and get perfectly respectable output.You can also get into the details, using gdb:$ gdb flighttimes-dbg

  • Using GDBgdb will open a prompt


    Keywords:breakstart, runstep, nextprintcontinue

  • Using GDBDebuggers have the concept of a breakpointNo matter what the code is doing, adding a breakpoint to your code will force the program to halt when it reaches that line

    (gdb) break Main.cpp:9

    ...will add a breakpoint at line 9 of Main.cpp

  • Using GDBSet up several breakpoints where you think that your code is probably having problems, or where youd like to see the contents of the variablesYou can add as many breakpoints as you likeon every line is overkill, but it wont hurt youOnce the code hits the breakpoint, you have several options

  • Using GDBSet up breakpoints before you start the programWhen youve got your breakpoints, type start or run at the command line, and the code will start running until it hits a breakpoint(gdb) startYou can also use start / run to restart a currently running program if you want to go back to the beginning

  • Using GDBWhen the execution hits one of your predefined breakpoints, itll stop and youll have the option to give more commands to the debugger

    Breakpoint 1, main () at Main.cpp:99 cout

  • Using GDBIn order to make the line run, we can use the step and next commandsThese are effectively equivalent, they move execution forwardBUT one crucial difference:next moves forward in the same function (in this case the main function). after one line, itll stop again and wait for a commandstep will stop after one line, but if there are any function calls on the line, itll step into them and stop there

  • Using GDBSometimes youll get a blank line as a prompt thats because youre still running your program, and so anytime you have a cin >>, youll have to stop and enter input

    15 bool success = (cin >> beginTime >> endTime);(gdb) next1030 1415

  • Using GDBThe step command moves into a function

    17 ft = new FlightTime(beginTime, endTime);(gdb) stepFlightTime::FlightTime (this=0x804d008, departHHMM=1030, arrivHHMM=1415) at FlightTime.h:1212 FlightTime();There are a couple of things to look at here:FlightTime::FlightTime name of functionthis=0x804d008 thats the memory address of the ft object weve just createddepartHHMM, arrivHHMM are the arguments to the functionFlightTime.h:12 is where were stopped line 12 of FlightTime.h

  • Using GDBBe a little careful when you use stepIf you have a function that belongs to a standard library like string or vector, itll go ahead and step into it, but the output will be completely meaningless to you(you can sort of decipher it but its really not worth it, you can go ahead and trust that the problem is on your end )

  • Using GDBIf you dont like what youre seeing, and you know the problem isnt here, you can type continue to get to the next breakpoint

    (gdb) continueContinuing.

    Breakpoint 3, main () at Main.cpp:2222 ta.add_flight(ft);(gdb)

  • Using GDBAfter running this line (with which keyword?), Id like to see what ends up inside the ta.flights vectorWe can use the print statement for that

  • Using GDBPrint tells you whats currently in the variableSometimes this will look meaningless: memory addresses are values for pointersOur vector is a vector of pointers to FlightTimes

    (gdb) print ta.flights$17 = std::vector of length 1, capacity 1 = {0x804d008}

  • Using GDBOften a simple print statement will be totally sufficient(gdb) print flightCtr$18 = 0We can use the dereference operator if we happen to have a pointer wed like to examine(gdb) next15 bool success = (cin >> beginTime >> endTime);(gdb) next1640 235517 ft = new FlightTime(beginTime, endTime);(gdb) next21 if(success && ft->isValid(invalid_num)) {(gdb) print ft$19 = (FlightTime *) 0x804d030(gdb) print *ft$20 = {beginTime = 1000, endTime = 1435, valid = true, invalid_num = 0}

  • Using GDBArrays make it pretty easy to be examined, since theyre just a stretch of memoryVectors are a little trickier we have no direct access to the array underlying it. But the debugger has a way around it!

  • Using GDB(gdb) print *(ta.flights._M_impl._M_start)@ta.flights.size()$24 = {0x804d008}(gdb) print *(*(ta.flights._M_impl._M_start)@ta.flights.size())$25 = (FlightTime *) 0x804d008(gdb) print *(*(ta.flights._M_impl._M_start)@ta.flights.size())[0]$26 = {beginTime = 630, endTime = 855, valid = true, invalid_num = 0}

    Its a little ugly, but it gets you where you need to be.

  • Using GDBYoull even be able to call some functions, but not all

    Remember that if you have a pointer to 0, its a null pointer and shouldnt be dereferencedFortunately gdb is smarter than our plain compiler and you wont get a segfault

    (gdb) print *ftCannot access memory at address 0x0

  • Flight Times ProgramWell walk through the differences between the code from yesterday and todays codeWell also walk through how to go about writing some of the crucial functions needed to get FlightTimes working