Tuesday, July 20, 2010

C Traps and Pitfalls

C Traps and Pitfalls - by Andrew Koenig, 1989, 160 pages. ISBN-10: 0201179288.

Andrew Koenig wrote a small reference manual on C programming based on his experience when he worked at AT&T lab. Since it received wide acceptance, he added more material and resulted in this book. It's highly recommended to any serious C programmers. Some contents are kind of outdated, such as those about problems of early day compilers. But many points are still ubiquitously applicable.

- Introduction

- Chapter 1. Lexical pitfalls
1.3 greedy principle in evaluating statement.
e.g. a --- b is: a-- - b,
y = x/*p is: y=x plus the start of a comment!
Should write as y = x / *p or y = x / (*p).
a +++++ b is: a++ ++ + b. This has compile error since a++ is not l-value.
Be careful, if a number starts with 0, it's an octal number.

- Chapter 2. Syntax pitfalls
2.1 Function declaration.
This casts address 0 as the pointer to function void f() and call it.
(* (void (*)()) 0)();
2.2 Operator precedence.
if (a == 'a' || b = 'b' || c == 'c') {} is equivalent to:
if ((a == 'a' || b) = ('b' || c == 'c')) {}
2.6 dangling else.
if (a)
if (b) b ++;
else { c ++; }
is equivalent to:
if (a) {
if (b) b ++;
else { c ++; }
}

Chapter 3. Semantic pitfalls
3.6 off-by-1 error - is the most prevalent.
One solution in C: 0-based C array, using asymmetric boundary.
e.g. x >= 16 && x < 38. 38-16 is exactly the size of the range.
3.9 Check overflow.
method 1: if ((unsigned) a + (unsigned) b > INT_MAX) // overflowed
method 2: if (a > INT_MAX - b) // overflowed

Chapter 4. Linking
lint.

Chapter 5. Library functions
5.1 int getchar() - return types does not match function, can cause problem.
5.2 I/O buffer.
setbuf(stdout, (char *) 0); // no buffer
5.4 errno
5.5 signal, longjmp

Chapter 6. Preprocessor
- macro
- use of -- or ++ can cause unexpected side effects in macro.
6.3 assert
#define assert(e) \
((void) ((e) || _assert_error(__FILE__, __LINE__)))

Chapter 7. Portability
7.5 >> and divide by 2. >> is faster and works correctly when var is positive.
e.g. replace "mid = (left + right) / 2" with "mid = (left + right) >> 1".
7.11 literal string can represent an array.
e.g. "0123456789"[n%10] is equivalent to: a[] = "0123456789"; a[n%10];
This is used in systems where 0-9 may not be consecutive.

Chapter 8. Suggestions and answers.
- Optimize binary search:
1) use >> instead of 1/2, 2) use pointer instead of array indexing.
- Little/big ending.
- getchar(), putchar() have both macro and function definitions. macro is faster.
- atol().

Appendix A
- printf. %p, %n, %06d is equivalent to %.6d in most cases.
- varargs
- stdarg

Author's suggestions on using C++:
- avoid pointer
- use library
- use class

No comments:

Blog Archive

Followers