Robust C

Rule of Thumb

If there's any way you can avoid writing it in C or C++, then avoid it. The potential for slight performance gains is almost always nowhere near worth the cost of an application that could explode at any time.

That being said, sometimes C really is the right tool for the job...

Vital Tools

  • GDB: The GNU Debugger
  • Valgrind: Program analysis tool, excellent for checking memory access

Potentially Useful Tools

Memory Management

Tools to catch or eliminate memory leaks.

Static Checking

Static analysis of source code to catch certain classes of errors.

Concurrency

  • Always favor multiple processes over multiple threads
  • Avoid using shared memory.
  • Try to use a communication mechanism such as pipes or posix message queues over shared memory, even for threads in the same process.

GCC Options

DON'T IGNORE COMPILER WARNINGS!

Important Note: some of the gcc stack-checking options will cause valgrind to report writes to unallocated memory. You've been warned.

  • -Wall, -Wextra, -Wpointer-arith: make GCC complain more about bad things you could be doing
  • -fstack-protector, -fstack-protector-all: Detect some buffer overflows/stack smashing
  • -fstack-check: Helps the OS catch programs that exceed stack bounds. Mostly useful for multi-threaded programs.
  • -fbounds-check: Bounds check arrays (only good in gfortran and gcj)
  • -ftrapv: Trap when overflow occurs.