Advanced C Programming

Autumn 2015 :: ECE 264 :: Purdue University

This is for Fall 2015 (9 years ago)

Exam #1 study guide

Exam #1 will cover everything we have done up to the exam date, including homework, lectures, and the relevant parts of the reference sheet.

You will be expected to know all vim and gdb commands on the course reference sheet and in the gdb tutorial reading—including those that were not directly discussed in lecture or used in the homework.

This exam will be closed book. You may not use the reference sheet or anything else. See the course policies for other policies regarding exams. In particular, bring photo ID and use the bathroom in advance. Check your email before coming, as there will be assigned seating.

How to study

  1. Make sure you can describe the meaning and purpose of every item in the list below.
  2. Number bases: Review hw02, hw03, and hw04.
  3. Memory: Draw out a call stack, showing the state just before a correct swap function returns. (Swap functions were covered on 9/17 and are easily searchable online as an example of call-by-address.) Do hw04.
  4. Dynamic memory: Draw out the call stack and other memory for the main function, just after smintf returns.
  5. Memory problems: Write some minimal example programs (similar to the back of the reference sheet). Compile and run each in Valgrind. Make sure you understand the output.
  6. Code standards: For each item under code standards (below), make sure you understand the reason, and can give a good and a bad example. Reasons include readability, maintainability, and preventing specific types of bugs or mistakes, depending on the item.
  7. Vim: Open a file in vim and run through every command in the list below. Make sure you know its effect. Possible exam questions might give you a sequence of commands and ask you to predict its effect, or give you a before and after block of text, and ask you to give vim commands to efficiently transform one into the other.
  8. Gdb: Try every command in the list below. (The list is drawn from the tutorial reading, reference sheet, and hw04.) Make sure you understand what it tells you, and the output. Do hw04 (obviously).

Topics

The list below includes nearly all of what we have covered so far. A few additional topics might be added after lecture on Tuesday September 22. See the bottom of this page for a list of any changes. No new topics will be added after that date.

Conceptual

Memory

number bases meaning, conversion, role in C programs, relation to printf, internal representation, printing vs. parsing
dynamic memory malloc(), free(), best practices
stack frames what is included?, what is not?, what happens when function is called?, what happens when it returns?, how are arguments passed?
memory problems leaks, buffer overflow, buffer overread, dangling pointer, null pointer read/write, uninitialized values, double free
memory segments stack, heap, text segment, data segment, bss segment, What goes in each segment? (This article summarizes what was said in class.)

The C System

header files purpose, usage
build process preprocessor » compiler » linker

Software engineering

Test-driven development process, benefits
How to write bug-free code DRY rule, learn tools, broken windows, sleep, nutrition, distractions

Programming with memory addresses

__ gets the address of __
__ gets the value at __
__ gets the address of a new allocation block sufficient for 3 ints __
__ is the address of a __
*a vs. a[0]

Code quality and bug avoidance

declarations declarations initialize, why?
NULL vs. 0 vs '\0' use what you mean, why not 0 for all?
boolean values true, false, stdbool.h, why not 1 and 0?
dead code unreachable code, useless variables, useless assignments
variable names constants, helper functions, meaningful names, case
array initializers why?
sizeof no assumptions about type sizes, avoid sizeof(my_variable), why?
consistency braces, indenting, naming, spacing
# not on exam #1: vim modeline, struct names, assert, alternatives to printf for debugging

Tools

Debugging with gdb

Controlling execution continue, next, step, until, finish, call
Starting/stopping run, core, quit, kill, set args
Breakpoints break, info breakpoints, tbreak, disable, break, ignore
Viewing context list, backtrace, frame [{frame_num}], down, up
Exploring a stack frame info frame, info locals, info args
Viewing memory x (including the flags on the reference sheet), print, whatis, display
Modifying memory set var
Watchpoints watch, rwatch, awatch
Other help

Editing code with vim

motion, by character h, l, k, j
motion, to line start/end 0, $, ^
motion, to specific line gg, G, {line#}G
motion, by word w, e, b
motion, with marks m{a-z}, '{a-z}
motion, searching /{pattern}, *, #, %, n, p, :noh
delete dd, d{motion}
yank yy, y{motion}
change cc, c{motion}
indent/dedent >>, >{motion}, <<, <{motion}
lowercase/uppercase gugu, gu{motion}, gUgU, gU{motion}
indent code ==, ={motion}
add characters/lines i, I, a, A, o, O
put (paste) p, P
visual select v, V
undo/redo/repeat u, ^R, .
quick macros q{a-z}, q, @{a-z}
write (save) :w
edit :e, :tabe, :split
replace :%s/{from}/{to}/gc
help :h {topic/cmd}
quit :q

Updates

9/19: Added display command under Debugging with gdb, boolean values.

9/20: Reformatted; added introduction and tips; added or expanded several items, including C programs (section), memory problems, boolean values; changed environmental factors with how to write bug-free code (from reference sheet), number bases