Advanced C Programming

Autumn 2016 :: ECE 264 :: Purdue University

This is for Fall 2016 (3 years ago)

Exam #1 study guide for section 2 (Quinn)

The scope of exam #1 will include everything we have done up to the exam date, including homework, lectures, and the relevant parts of the reference sheet—except Vim. (There will be a quiz on Vim at a later date after the exam.)

Before the exam, you must do a few things.

  1. Read the policies to learn the rules (e.g., what items are allowed, etc.).
  2. Check your email the morning of the exam for your assigned seat number.
  3. Bring photo ID.
  4. Use the restroom. Bathroom breaks are not allowed.

Topics

The questions on the exam will be drawn from the topics below. The questions may be a little different but will be in the same spirit. Items beginning with the word "Explain" would most likely be distilled into a simpler question with very short answers.

  1. Number bases and ASCII. You should be able to:
    1. Convert a number from any base to any other base (between 2 and 36). Example: convert 0x3f (hexadecimal) to base 27.
    2. Understand the relationship between (a) number base used to express numbers in your code, (b) representation in memory with no number base, and (c) number base used to print numbers on the screen.
    3. Understand the relationship between digit characters (e.g., '5') and the values they traditionally represent (e.g., the result of two plus three).
    4. Given a single character (e.g., 'A') show how it would look when stored in memory.
    5. Explain why 6 * 8 == '0'.
    6. Write a print_integer(…) function that works with only integers between 0 and 9.
    Your knowledge of number bases will draw on discussions and demonstrations in class, and your experience doing HW02.
  2. Variadic functions. You should be able to:
    1. Write simple programs using variadic functions. For example, make a function that takes one or more characters as arguments and prints them as a string.
    2. Understand the difference between accessing arguments in a variadic function versus accessing elements from an array. What can one do that the other can't?
    3. Give an example of something va_copy lets you accomplish that you couldn't do otherwise.
    Your knowledge of variadic functions will draw primarily on your experience with HW04 and HW06, as well as the discussion in lecture.
  3. Memory. You should be able to:
    1. Given a small program, draw the call stack, including the parameters, return address, and local variables. For the return address, it's okay to be vague (e.g., "somewhere in main(…)).
    2. Given a small program, say what data is in the stack segment, heap segment, data segment, and text segment. (We won't ask about the BSS for exam #1.)
    3. Fill in some missing values in a GDB session. For example, given an x/… command examining a string or array, fill in the expected output (to show that you understand what is in memory).
    4. Write a very small program that allocates and deallocates memory for an array and/or a single value on the heap. For example, write a program that allocates an array of 3 integers, fills it with values, and then frees it.
    5. Given a small program that uses malloc(…) and free(…), draw the contents of the stack and heap at a given point in the execution.
    6. Detect and fix any of the types of memory faults listed on the back of your reference sheet. (We have touched on some of these already, but we will go over all of them on Mon 9/19. However, most should be self-explanatory from the examples given on your reference sheet.) To practice, try to create buggy programs that illustrate those problems. Then, compile your buggy programs and run them in valgrind.
    7. Read and write code that uses memory addresses, arrays, or strings, including any of the syntax in your reference sheet under addresses (pointers), arrays, and strings.
    8. Describe code using memory addresses in English phrases such as "__ gets the address of __," "__ gets the value at __," "__ gets the address of a new allocation block sufficient for __," and "store __ at address __.".
    9. Find the bug(s) in a small program that uses memory addresses and/or malloc(…).
    10. Use arithmetic operators with memory addresses to find the i'th element of an array, either on the stack or in the heap. (It doesn't really matter.)
    11. Predict the output of a function that receives a memory address as a parameter and uses it to modify local variables of the caller and/or memory on the heap.
    12. Predict the output of the sizeof(…) operator for a given expression (e.g., sizeof('0')).
    Your knowledge of memory concepts, syntax, and descriptive language will draw from discussions in lecture, in-class exercises on 9/14/2016 and 9/16/2016, and your experiences with HW04 and HW06 Nearly all of the required information is on your reference sheet.
  4. Code quality. You should be able to:
    1. Given some poorly-written code, identify and/or fix the problems relative to the Code Quality Standards used in this class.
    2. Given a code quality problem, explain why it might lead to bugs and/or make the code more difficult to read or maintain.
    Your knowledge of code quality will draw from reading the Code Quality Standards (as directed in the first step of HW02), discussions of specific issues in class (e.g., sizeof(…) and 'A' versus 65), and applying these standards on HW02, HW04, and HW06.
  5. GDB. You should be able to:
    1. Know the following commands, including how to use them and what output to expect: backtrace, break, continue, display, down, finish, info locals, info args, info breakpoints, help, list, next, return, run, set args, step, until, up, watch, whatis, p/…, and x/…. That list consists of the ones on the reference sheet, plus display and undisplay, which were covered in HW05.
    2. Explain the conceptual and practical differences between the p/… and x/… commands?
    3. Explain the conceptual and practical differences between breakpoints and watchpoints.
    4. Explain what kinds of bugs GDB can help you find, and what it can't.
    5. Given a description of a need (e.g., "run the program, break at line 25, and print the value of n as hexadecimal"), give the appropriate commands.
    6. Given a GDB session with the commands removed, give the command that would produce the given output.
    7. What command will print the type of variable a?
    8. What command will cause gdb to break (stop executing your program) whenever the value of n changes?
    9. What command will display all local variables in the current frame?
    Your knowledge of GDB will draw from HW05 (including the required GDB tutorial reading), demonstrations in class, and using GDB to debug your code for HW06. You might need to do a modest amount of additional practice to get solid with any commands on that list that were in the reading but not covered in HW05. In particular, watchpoints were in the reading but not the homework.
  6. Test-driven development. You should be able to:
    1. Given a planned sequence of steps for constructing a program, find any flaws in the plan.
    2. Given a description of a program to be built, write out the first few steps, following TDD.
    3. Explain the benefits of TDD.
    4. Explain how you used (or could have used) TDD on one of the assignments so far.
    Your knowledge of TDD will draw from discussions in lecture, HW03 (including the write-up in the description), and your experience applying it on other assignments. You are not expected to know more specific details about how TDD is applied outside this class. We are sticking with a very narrow version of TDD.

Lecture on 9/19/2016

The lecture on 9/19 will go over memory faults and programming with addresses, and a demonstration of the few GDB commands that are in scope for this exam but were not directly applied in HW05.

Practice exam

Here is an old exam from Fall 2015 and a solution.

Obviously, the exam for this semester will be different, but most questions will be similar in style. The coming exam may also use some of the formats used in quizzes and in-class exercises this semester.

Not in scope

The following are not in scope for this exam.

Feel free to ask whether other topics not mentioned here will be "in scope" for the exam.