common bugs in C programs

Malcolm Rix malkie at hpqtdla.HP.COM
Mon Jan 8 19:13:49 AEST 1990


Here is a list of things we found to be common mistakes programmers made here
at QTD - some of them are general programming errors rather than specific to
C

			/V\alkie

===============================================================================

As we all know 'C' is a language riddled with potential problems and
pitfalls for the unwary. While developing code and reviewing the work of others
in the common firmware group we have been collecting a list of the most
frequent problems:

(1)  Misinterpretation of number bases (i.e. 0x12 != 12 or 0x01000 != 8).
(2)  Loop initialisers are placed within the loop.
(3)  Problems releating to undeclared extern functions or parameters defaulting
     to type 'int'.
(4)  Float constants present in code without '.' or 'E' in the number.
(5)  Misinterpretation of the conversion processes in mixed type expressions.
     (esp. Where 'int', 'float' and 'double' are combined.)
(6)  Forgetting equality test is '==' rather than '=' (a common error for ex.
     PASCAL programmers).
(7)  Leaving '&' off variable parameters to functions.
     I.e. Writing 'scanf("%d\n", i);' rather than 'scanf("%d\n", &i);'.
(8)  Uninitailised variables getting forgotton due to side effects of RAM tests.
     (It is a good idea to set the memory to a range of values before testing).
(9)  Calling a function with the wrong number of parameters.
(10) Calling a function with the wrong type of parameters.
(11) Confusing '&' and '&&' in boolean expressions.
(12) Using /* in an expression and accidently starting a comment.
     E.g. x = 3/*i; 
(13) Ommitting 'const' from ROM data.
(14) Assuming x<<2 is the same as x*2. (Its actually x<<1).
(15) Substituting shifts for multiplies by powers of 2 in expressions
     without taking taking care of change in operator precedence.
     The expression:       y = x*2 + 4; 
     Should be rewritten:  y = (x<<1) + 4; 
     And not:              y = x<<1 +4;
(16) Passing structures to functions as if they were arrays and assuming a
     pointer is passed (it is not, the struct is passed as a value parameter).
(17) Comparison of floats for equality.
(18) Finishing a "for( init;conditional; command)" statement with a semicolon.
(19) Forgetting to declare space for variable length string within list
     elements.
(20) Missing a semicolon off a typedef statement.
(21) Putting a semicolon on a #define statement.
(22) Getting complex declarations wrong - e.g. A pointer to an array of
     pointers to functions returning pointer to float etc.
(23) Forgetting *(a+i) is the same as a[i] so that *(a+10) doesn't access
     the 11th byte of `a' but the eleventh entry in an array of structures.
(24) Forgetting address arithmetic is done in multiples of the size of
     the objects to which the pointers point.
     I.e: int *a;   a++;  /* Points to next WORD not next BYTE */
(25) Confusing (a.b) and (a->b).
(26) Misunderstanding dereferencing of things like *(a->b).


Malkie
Rana



More information about the Comp.lang.c mailing list