Presentation is loading. Please wait.

Presentation is loading. Please wait.

ecec 201 advanced programming for engineers

Similar presentations


Presentation on theme: "ecec 201 advanced programming for engineers"— Presentation transcript:

1 ecec 201 advanced programming for engineers
problem set 2 andrew r. cohen due at end of lab during your assigned lab sessions submission accepted until beginning of class next tuesday

2 ground rules for this problem set, you can use vs code with your virtual box, or vs code with wsl (windows) or vs code native on linux. mac users can use vs code with gcc/gdb from the Xcode tools in other words, you need vs code + gcc (and gdb). use whatever os / environment that works for you use only the websites specifically referenced in this document, and your textbook. no google. no stack exchange. no collaboration or discussion with *anyone* except Prof. Cohen or the TA’s. no exception.

3 know vs code read this webpage open your program helloWorld from last week (or create a new one) using the vscode tools, find the definition of the printf function try hovering over the printf command with your mouse, and verify that the definition pops up put the cursor on the word ‘printf’ and press F12 (or right click and select ‘go to definition’) Problem 0: what is the prototype (definition) for the printf function

4 return values Question 1: what can you tell about the return value of the printf function from its prototype (definition) from the vscode terminal, enter ‘man printf’ screenshot here in the textbook find the appendices what is the C operator symbol for the bitwise complement? find the appendix listing the standard library functions THIS IS SUPER USEFUL. BOOKMARK THIS! look up the printf function in the textbook appendix defining stdlib functions read appendix D (standard library functions) to familiarize yourself with the list – you will be expected to know these Question 2: what is the return value of the printf function?

5 modify hello world run the program and make sure it works as expected

6 breakpoints THE MOST VALUABLE AND IMPORTANT WAY TO VERIFY THE CORRECTNESS OF YOUR CODE IS TO STEP THROUGH IN THE DEBUGGER LINE BY LINE! code is not done if you haven’t touched every line

7 set a breakpoint in vs code
click on the line with the printf statement from the debug menu, click ‘Toggle Breakpoint’ you must see a red dot appear on the line, to the left of the printf statement before you proceed!!! click on the red dot with your mouse verify it goes away click again where the red dot was to the left of the line number verify the red dot shows up again clicking like this is the usual way to set breakpoints

8 conditional breakpoints
right click on the red dot (breakpoint marker) and choose ‘edit breakpoint’ this is a very powerful way to control when your program breaks for now, enter ‘1’ and run your program (F5) next, right click again, edit breakpoint and enter ‘0’ for the condition run the program again Question 3: how does entering 0/1 for the breakpoint condition impact the program flow?

9 vs code debug tools set a breakpoint on the printf line
run the program (f5) till the breakpoint on the debug tab, find the ‘locals’ window Question 4: what is the value of the pszHello variable? note 1 – this is a big deal. char *pszHello means that pszHello is a ‘pointer’ to a character. In other words, pszHello is a memory address. The contents of that memory address are a character. note 2 – we’re allowing the precompiler here to allocate space for our pszHello string. note 3 – more on this in chapters 11 and 17 Question 5: what is the value of the *pszHello character?

10 modifying variables set a breakpoint on the printf line and run to it
in the vscode locals window, expand the pszHello variable so the *pszHello value is shown double click the ‘h’ (or right click and choose ‘modify’) type ‘x’ instead press F10 to step over the printf command Question 6: screenshot

11 aha! vs code is lacking… here we have our first valid complaint
vs code for c/c++ is missing tools to effectively view and manipulate memory the visual studio ide is far more sophisticated at this visual studio allows you to view and modify variables in memory with far more ease and power then is possible here… …but visual studio is heavy and closed source, so we’ll just keep on keepin’ on… for more powerful debug tools, we can use gdb

12 debugging with gdb from the terminal window in vs code (or from a stand-alone terminal) enter ‘gdb [program name]’ e.g. gdb HelloWorld then enter ‘run’ Question 6A: screenshot to show output

13 gdb see gdb.pdf on the website for details
enter ‘break 4’ ([4] use the linenumber of your program corresponding to the declaration of the pszHello variable) enter ‘run’ you should see the program hit the breakpoint enter ‘print pszHello’ enter ‘step’ Question 7: what is the value of pszHello before and after the line?

14 manipulating variables with gdb
run your program again, breaking at line the printf line view the memory at address pszHello run ‘x /20cb pszHello’ see gdb.pdf pp for details screenshot Question 8 : how does the printf command know when to stop printing characters for pszHello? from the breakpoint, enter : set *((char *) pszHello)=‘y’ enter ‘continue’ redo this, but this time, modify the memory so the program prints ‘hello andy’ Question 9: screen shot

15 debugging and bash printf is one of the most useful debugging tools…
redirecting output to a file can be helpful as well to do this, use the ‘>’ operator in the shell from a vs code terminal, execute your program note that the current folder is not on your path (by default), so you may need to execute ‘./helloWorld’ now redirect your output to a file ‘./helloWorld > hello.txt’ ‘cat hello.txt’ from the console, enter ‘man tee’ the tee utility allows you to redirect to stdout and a file: ‘./helloWorld | tee helloTee.txt’ cat helloTee.txt note the ‘|’ pipe operator – this redirects the output of a command to the stdin of another command read pp in linuxfun.pdf Question 10: write a shell command to make a sorted list of all the files in /etc that contain the string conf in their file name


Download ppt "ecec 201 advanced programming for engineers"

Similar presentations


Ads by Google