bug me now / bug me later
Dolf Grunbauer
dolf at idca.tds.PHILIPS.nl
Thu Jun 14 23:44:35 AEST 1990
In article <340021 at hplvli.HP.COM> boyne at hplvli.HP.COM (Art Boyne) writes:
<jharkins at sagpd1.UUCP (Jim Harkins) writes:
<
<>Lets say you have the following (you ADA people, please bear with me):
<>
<> #define SIZE 6
<> #define STEP 2
<>
<>Which is better:
<>
<> a. for(i = SIZE; i != 0; i -= STEP)
<>or
<> b. for(i = SIZE; i > 0; i -= STEP)
<>
<>Where this makes a difference is suppose SIZE is changed to 7.
<>'a' goes into an infinite loop, while 'b' stops.
<
<I would consider the following preferable:
<
< if (SIZE % STEP != 0)
< assert("SIZE not multiple of STEP in function ...");
< for (i = SIZE; i > 0; i -= STEP)
How about:
#if (SIZE % STEP) != 0
SIZE not multiple of STEP
#endif
As now the compiler complains at compile time or your code will
run faster. The compiler gives the line number as the line will
probably not be valid C. I am aware that this trick will not work when
SIZE & STEP (or at least one of them) are variables rather than macro's.
--
Dolf Grunbauer Tel: +31 55 433233 Internet dolf at idca.tds.philips.nl
Philips Information Systems UUCP ...!mcsun!philapd!dolf
Some kind of happiness is measured out in miles
More information about the Comp.lang.c
mailing list