Author: Vincent Diepeveen
Date: 11:48:30 07/04/03
Go up one level in this thread
On July 04, 2003 at 13:56:09, Normand M. Blais wrote:
>Hi,
>
>First, I would like to thanks everybody for their help. No one can succeed
>alone.
>
>The problem that I had was that my program would not run properly when compile
>with optimization for speed but would run ok in debug version or in release
>version with default optimization. I found that the problem was with a specific
>function and the way it was coded:
>
>// This fuction runs with no problem in debug version or release version with
>// default optimization.
>// But it fails in release version with optimization for speed.
>inline int s2i(const char *str)
>{
> // input: a square name (string) (ex: e4)
> // output: a square number (integer) (ex: 36)
>
> int sq = (*str - 'a') + (8 * (8 - (*(++str) - '0')));
you make a bunch of mistakes here.
Of course Dieter already showed you clearly one. But the real beginners mistake
here is that you try to solve the world at one line.
What is easier to understand (if it would be correct code):
a = (((((4*5+20)((((a*5))+(5-(((((5*45)))))));
or:
a = 4*5;
a += 20;
a *= 5;
a += 5*45;
I bet i know the answer. Divide and conquer.
All the bugs come from not writing simple stuff at 1 line. If you are a good
programmer you *can* write tough code at 1 line. If you aren't then keep away
from it. If you are real good you usually try to keep away from it :)
Another thing which you do and which under ansi-C rules usually is allowed, but
usually it goes wrong is that you mix different types.
if you say what you wrote then you are mixing 8 bits characters with 32 bits
integers. Usually that is not a good idea. Sometimes you can do it. But try to
keep away from it. If you always keep away from it you won't make a mistake with
it. Avoid next:
int a;
char b;
....
if( a >= b ) <== is that clever code?
.....
if( b >= a ) <== is that clever code?
.....
>
> if (sq >= 0 && sq <= 63) return sq;
>
> return -1;
>}
>
>
>// This function runs fine (with or without optimization).
>inline int s2i(const char *str)
>{
> // input: a square name (string) (ex: e4)
> // output: a square number (integer) (ex: 36)
>
> int sq = (str[0] - 'a') + (8 * (8 - (str[1] - '0')));
>
> if (sq >= 0 && sq <= 63) return sq;
>
> return -1;
>}
>
>
>Why the first version of the function causes problem is not clear to me. Anyway.
>I felt that it is fare for me to share this information taking into account that
>I benefit greatly from others.
>
>Thank you,
>
>Normand
This page took 0 seconds to execute
Last modified: Thu, 15 Apr 21 08:11:13 -0700
Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.