Advanced C Programming

Spring 2020 ECE 264 :: Purdue University

⚠ This is a PAST SEMESTER (Spring 2020).

Exam #1 study guide

The exam is Wed, Feb 19 at 8-9pm in PHYS 112. This will be a 1-hour exam. Additional time might be permitted, at the instructor's discretion, if there is a need and the room is available. Seat assignments will be emailed to you sometime before the exam (typically in the afternoon). Before you come, ① check your email, ② use the bathroom, and ③ bring your photo ID.

You are responsible for reading the syllabus, including the section on “Exams”. Information on what is allowed and what we provide is there.

Conflicts. If you have a conflict with the exam 1 time, please send me email with subject “Exam 1 conflict - ECE 264” and your availability on Thu 2/20. Please do that by end of Sun, if possible. You should hear back with a time and place on Mon 2/17.

DRC scheduling. If you will take the exam in the DRC, please schedule it for Thu 2/20. Send me email ASAP with subject “Exam 1 conflict - ECE 264” if that is impossible.

Scope

The scope of exam 1 contains everything we've covered so far, except Vim, including the following:

  1. lectures up to 2/17 + HW02, HW03, HW04, HW05.
  2. number representations, address syntax, memory, strings, code quality, GDB, preprocessor, TDD, make, testing
  3. no Vim
  4. past exams/quizzes are on the course web site under Resources.

Format

The format of the actual questions will be similar to past exams: mostly short-answer, with the possibility of some circle/cross-out or multiple choice questions. You will not be asked to write long sections of code. However, you might be asked to write expressions or very small segments of code (e.g., a few lines).

Questions about the contents of memory may use this memory form or ask you to fill in the output of a GDB p/ or x/ command.

Topics

  1. Data representations: number bases, 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.
    7. Understand that variables in memory have no base. For example, if you are given code with an int variable, and asked what that variable's number base is, the correct answer would be “none” or “n/a”.
    Your knowledge of number bases will draw on discussions and demonstrations in class, and your experience doing HW02.
    You will be given an ASCII table on the exam.
  2. 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, 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. 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.
    5. Describe code using memory addresses in English phrases such as "__ gets the address of __," "__ gets the value at __," and "store __ at address __.".
    6. Given a flawed initialization (e.g., char* ch = 'a';) fix it by modifying the expression (RHS) to match the type on the LHS.
    7. Given an expression, give its type.
    8. Use arithmetic operators with memory addresses to find the i'th element of an array.
    9. Predict the output of a function that receives a memory address as a parameter and uses it to modify local variables of the caller.
    10. 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, and your experiences with HW04. Nearly all of the required information is on your reference sheet.
  3. 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. What do variadic functions allow you to do, that wouldn't be possible by passing an array to a function?
    3. What can you do with an array passed to a function that would be more difficult if you used variadic arguments?
    4. Give an example of a variadic function that you have used before ECE 264.
    Your knowledge of variadic functions will draw primarily on your experience with strings and HW06, as well as the discussion in lecture.
  4. GDB. You should know all GDB commands on the reference sheet, and be able to answer questions about memory, types, and the practice of debugging in the context of GDB. For example:
    1. Explain the difference between the value of an expression (e.g., result of p a_o) and the data at the address returned by that expression (e.g., x/4bx a_o).
    2. Explain what kinds of bugs can be found by stepping through code and inspecting stack frames in a running program. … and what kinds of bugs cannot be found in that manner.
    3. Give the type of an expression (e.g., *(a_o -> b)[2]) in terms of the output of the whatis command (e.g., whatis *(a_o -> b)[2]). Such a question is testing your knowledge of types, not GDB.
    4. Explain the conceptual and practical differences between breakpoints and watchpoints. (The concepts of breakpoints and watchpoints are not GDB-specific; most debuggers have them in some form.)
    5. Suppose you set a breakpoint and a watchpoint at the same line of code. Which will potentially lead to more stops?
    6. Given a bug scenario, which of the following would be most useful: breakpoints (break), watchpoints (watch, etc.), or stepping through and observing (display)?
    7. Fill in the blanks of the info args and info locals commands. Such a question is testing whether you can distinguish between arguments and local variables.
    8. Write the contents of memory at some address if interpreted as chars (1 byte units), half words (2-byte units), words (4-byte units), or giant words (8-byte units), taking into account the byte order of our little endian platform. This might be phrased like “Fill in the output of x/4wd a.”
    9. Given the output of a x/ command, give the command that produced it.
    10. Commands to know:
      Start set args
      Breakpoints break, clear, delete, info breakpoints
      Watchpoints watch, awatch, rwatch, info watchpoints
      Automatic display info display, display, undisplay
      Explore the stack frame backtrace, down, frame, info args, info frame, info locals, list, up, whatis
      Controlling execution continue, finish, jump, next, return, run, set variable, step, until
      Reverse debugging record, reverse-next, reverse-step, and so on…
      View variables and memory print, x, units (b h w g), formats (d x s f c u o t z a)
    Your knowledge of GDB will draw from HW03 (including the required GDB tutorial reading), demonstrations in class, and using GDB to debug your code for the other assignments.
  5. Software engineering You should be able to:
    1. Code quality
      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 what kind of bugs or other problems it could lead to.
    2. Test-driven development
      1. Know the basic process described in the HW02 description and in lecture on 1/24/2020.
      2. Given a planned sequence of steps for constructing a program, find any flaws in the plan.
      3. Given a description of a program to be built, write out the first few steps, following TDD.
      4. Explain the benefits of TDD.
      5. Explain how you used (or could have used) TDD on one of the assignments so far.
    3. Assertions
      1. Given a bug scenario, show how an assert(…) statement could have revealed the bug sooner.
      2. For what purposes should assert(…) not be used.
      3. Given some code with several assert(…) statements, cross out the ones that violate the rules for assert(…).
    4. Make
      1. Diagnose an error in a Makefile.
      2. Given a Makefile and a make command (e.g., make debug) predict what commands will be executed.
      3. Fill in the blanks in a partially written Makefile.
      4. Explain the advantage of using a Makefile versus directly typing commands in bash.
    Your knowledge of code quality, TDD, and assertions will draw from your practice on homework assignments, discussions in lecture, and your reading of the Code Quality Standards.
  6. Preprocessor You should be able to:
    1. Explain the three main stages of compiling your source code to an executable (preprocessor, compiler, and linker).
    2. Explain what a #define macro can do that a regular function can't—and vice versa.
    3. Write a #define macro to replace a simple function—and vice versa.
    4. Predict the output of the preprocessor on a program including #define, #▒▒▒, #ifdef, #ifndef, #else, #endif, #include, and/or __LINE__.
    5. Fill in the blanks in a partially written .h file that uses the directives above.
    6. Add a #include guard to a given header file.

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.

Updates

2/16/2020
  • Added HW05 to the list of homework assignments in scope.
  • Added make as a topic in scope.
  • Added an explicit list of GDB commands to know. (This is for convenience, and is not a change. The study guide already said, “You should know all GDB commands on the reference sheet…”. That was also announced in lecture on Wed 2/5.)
  • Removed “1-hour exam, but room is scheduled for 2 hours*”
  • Added more sample question starters for test-driven development (TDD).