Mail Archives: djgpp/1996/06/14/22:44:01
Mark Habersack (grendel AT ananke DOT amu DOT edu DOT pl) wrote:
[...]
> len += CharWidth(*walker++, curFont);
> Here, the walker variable has been everytime incremented TWICE, not
> ONCE as it was meant to be - what is wrong?
> Later on I changed the offending line to
> len += CharWidth(*walker, curFont);
> walker++;
> and everything worked just fine. Where lies the error - is it my
> ignorance or a bug or side effect?
This heavily looks like a case of macro parameters with
side-effects. If CharWidth is a macro that evaluated its first
parameter twice, this would be a perfect explanation of the behvaviour
you describe. Just imagine Charwidth were something like this:
#define CharWidth(char, font) ((is_alnum(char)? (font)[char].width : 0)
Now, if 'char' is '*walker++', it will get incremented twice if
is_alnum(char) is true.
This is a well-known cause of problems with C, but can be hard to spot
sometimes. The fix is exactly the one you proposed (pass first, then
increment), and can be put as a rule like this:
Don't use an expression with side effects as a macro parameter!
Hans-Bernhard Broeker (Aachen, Germany)
- Raw text -