problems/risks due to programming language, stories requested

Dave Decot decot at hpisod2.HP.COM
Thu Mar 8 04:08:33 AEST 1990


> > 7. I had one bug caused by omission of an item in an initializer list
> > for a struct (a vector of function pointers).  The compiler could have
> > caught that if the language didn't allow partial initializer lists.
> 
> Caught by a feature, I'm afraid.  There are several methods you can use
> to help yourself catch this kind of mistake.  One is to do
> 
> 	some_type the_table[ /*note size not specified*/ ] =
> 	    { ... ... }
> 
> 	void check_the_table()
> 	    {
> 		assert(sizeof the_table == expected * sizeof the_table[0]);
> 		/* other tests */
> 	    }
> 
> Another trick is to use the preprocessor to help you check your counting.
> #define ten(A,B,C,D,E,F,G,H,I,J) A,B,C,D,E,F,G,H,I,H
> 
> 	some_type the_table[] = {
> 	    ten(
> 		ten(x00, x01, ..., x09),
> 		...
> 		ten(x90, x91, ..., x99)
> 	    )};
> 
> Now if you miscount, the preprocessor will complain.

And if you don't, it will introduce mysterious bugs related to the
eighth element in each row and column being the same as the tenth.

Please don't try to use the preprocessor to make C's design bugs look
better; it just makes it look much worse.

Dave



More information about the Comp.lang.c mailing list