Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: bugs, Bugs and BUGS!

Author: Uri Blass

Date: 15:10:47 05/27/04

Go up one level in this thread


On May 27, 2004 at 17:48:00, Richard Pijl wrote:

>
>>>>>>
>>>>>>1)I asked the computer to print the moves in the book only to discover that
>>>>>>printf("%s %s",move1,move2) does not work.
>>>>>
>>>>>Are move1 and move2 char* 's?  If so, that should be fine.
>>>>>What's happening?
>>>>>
>>>>>
>>>>>--
>>>>>James
>>>>
>>>>I do not know but for me it did not work and the computer printed the first
>>>>string twice instead of printing 2 different strings.
>>>>
>>>>Try the following code that is part of movei:
>>>>
>>>>
>>>>#define rankfrom(u) (((u)>>3)&7)
>>>>#define rankto(u)  (((u)>>9)&7)
>>>>#define filefrom(u) ((u)&7)
>>>>#define fileto(u) (((u)>>6)&7)
>>>>#define promotion(u) ((u)&(1<<29))
>>>>#define promote(u) (((u)>>16)&255)
>>>>
>>>>char *move_str(int move)
>>>>{
>>>>	static char str[6];
>>>>
>>>>	char c;
>>>>
>>>>	if (promotion(move)) {
>>>>		switch (promote(move)) {
>>>>			case KNIGHT:
>>>>				c = 'n';
>>>>				break;
>>>>			case BISHOP:
>>>>				c = 'b';
>>>>				break;
>>>>			case ROOK:
>>>>				c = 'r';
>>>>				break;
>>>>			default:
>>>>				c = 'q';
>>>>				break;
>>>>		}
>>>>		sprintf(str, "%c%d%c%d%c",
>>>>				filefrom(move) + 'a',
>>>>				rankfrom(move)+1,
>>>>				fileto(move) + 'a',
>>>>				rankto(move)+1,
>>>>				c);
>>>>	}
>>>>	else
>>>>	{
>>>>		sprintf(str, "%c%d%c%d",
>>>>				filefrom(move) + 'a',
>>>>				rankfrom(move)+1,
>>>>				fileto(move) + 'a',
>>>>				rankto(move)+1);
>>>>	}
>>>>	return str;
>>>>}
>>>>
>>>>printf("%s %s",move_str(1),move_str(0));
>>>>
>>>>//result b1a1 b1a1
>>>>
>>>>printf("%s %s",move_str(0),move_str(1));
>>>>
>>>>//result a1a1 a1a1
>>>>
>>>>Uri
>>>
>>>This is an easy one: your strings share one common place in memory, namely the
>>>variable static char str[6]. Therefore when you call move_str() for the second
>>>time, the first string gets overwrited.
>>>Filip
>>
>>I do not understand why they share one place in memory.
>>
>>The computer is supposed to do the following:
>>
>>1)calculate move_str(0)
>>2)forget every local varaible including str[6]
>
>This one is a local 'static' variable. It keeps its value.
>
>>3)calculate move_str(1)
>
>This will overwrite str.
>
>>4)forget every local varaible.
>>
>
>remember that an array variable is equivalent to a pointer in C. You're
>returning a pointer to the array str, not the string itself.
>
>return str;
>
>is equivalent with
>
>return &str[0];
>
>>Note that it seems to do it in that way when I do it in 2 different printf for 2
>>strings but not when I use one printf.
>
>First both functions are called, and then the string is built by printf, so you
>will get the same move twice.
>
>Richard.

I do not get the same move twice when I have
printf("%s ",move_str(0));
printf("%s ",move_str(1));

I thought that
printf("%s %s ",move_str(0),move_str(1));
should be exactly the same instruction.

I understand now that it is not and the second printf does not print move_str(1)
immediatly after it calculates it but calculates also move_str(0) and change the
value of move_str(1) by doing it.

I think that it is a bug in the language or in the compiler because it is clear
that the programmer mean the same in both cases.


correct compiler can solve the problem by translating

printf("%s %s ",move_str(0),move_str(1));
to
printf("%s ",move_str(0));
printf("%s ",move_str(1));

I see no logical reason not to translate it in that way from human point of
view.



Uri



This page took 0.01 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.