crafty bug?

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

jwes
Posts: 778
Joined: Sat Jul 01, 2006 7:11 am

crafty bug?

Post by jwes »

In crafty 23.2
line 540 of data.c is: int time_limit = 100;
line 570 of data.c is: int time_limit;
Why does this compile without error?
rbarreira
Posts: 900
Joined: Tue Apr 27, 2010 3:48 pm

Re: crafty bug?

Post by rbarreira »

Maybe the C standard allows it?

I was as surprised as you at first, but I just tried to compile this, and it works fine:

Code: Select all

#include <stdio.h>

int t=100;

int t;

int main &#40;void&#41;
&#123;
    printf ("%d\n", t&#41;;
    return t;
&#125;
However if the second definition of t contains an initialization, it fails (even if it's initialized to 100 again).

I tried with gcc, but I suppose crafty 23.2 has been compiled correctly by many compilers, which is why I suspect it's allowed by the standard.
bob
Posts: 20943
Joined: Mon Feb 27, 2006 7:30 pm
Location: Birmingham, AL

Re: crafty bug?

Post by bob »

rbarreira wrote:Maybe the C standard allows it?

I was as surprised as you at first, but I just tried to compile this, and it works fine:

Code: Select all

#include <stdio.h>

int t=100;

int t;

int main &#40;void&#41;
&#123;
    printf ("%d\n", t&#41;;
    return t;
&#125;
However if the second definition of t contains an initialization, it fails (even if it's initialized to 100 again).

I tried with gcc, but I suppose crafty 23.2 has been compiled correctly by many compilers, which is why I suspect it's allowed by the standard.
It is ok, although it was not intended and the second declaration has been removed...
Look

Re: crafty bug?

Post by Look »

Maybe the C standard allows it?
Hi,

I am not a geek, but try your code with these GCC flags and see what happens.

-ansi -pedantic -Wall -Wextra -Werror
rbarreira
Posts: 900
Joined: Tue Apr 27, 2010 3:48 pm

Re: crafty bug?

Post by rbarreira »

Look wrote:
Maybe the C standard allows it?
Hi,

I am not a geek, but try your code with these GCC flags and see what happens.

-ansi -pedantic -Wall -Wextra -Werror
Just tried it... gcc still doesn't have any complaints about the code.
Look

Re: crafty bug?

Post by Look »

rbarreira wrote:
Look wrote:
Maybe the C standard allows it?
Hi,

I am not a geek, but try your code with these GCC flags and see what happens.

-ansi -pedantic -Wall -Wextra -Werror
Just tried it... gcc still doesn't have any complaints about the code.
Compiler: Default compiler
Building Makefile: "C:\Dev-Cpp\Makefile.win"
Executing make clean
rm -f wtest.o warning_test.exe

g++.exe -c wtest.c -o wtest.o -I"C:/Dev-Cpp/include" -I"C:/MingW/include" -I"C:/Dev-Cpp/pthreads-w32-2-8-0-release" -ansi -pedantic -Wall -Wextra -Werror

wtest.c:5:5: error: redefinition of 'int t'
wtest.c:3:5: error: 'int t' previously defined here

make.exe: *** [wtest.o] Error 1

Execution terminated
rbarreira
Posts: 900
Joined: Tue Apr 27, 2010 3:48 pm

Re: crafty bug?

Post by rbarreira »

It also fails if I use g++ (with or without the strict options), but not with gcc.

Probably one more difference between C and C++.
Look

Re: crafty bug?

Post by Look »

rbarreira wrote:
Look wrote:
Maybe the C standard allows it?
Hi,

I am not a geek, but try your code with these GCC flags and see what happens.

-ansi -pedantic -Wall -Wextra -Werror
Just tried it... gcc still doesn't have any complaints about the code.
Yes, gcc.exe does not even give a warning. In fact C++ is supposed to cove C too, but apparently experts say there are subtle differences, like IIRC defining structures inside functions,... . But IMO it makes sense to write C code such that it would be compiled with both GCC and G++. That is, the C subset of C++.
rbarreira
Posts: 900
Joined: Tue Apr 27, 2010 3:48 pm

Re: crafty bug?

Post by rbarreira »

Look wrote: Yes, gcc.exe does not even give a warning. In fact C++ is supposed to cove C too, but apparently experts say there are subtle differences, like IIRC defining structures inside functions,... . But IMO it makes sense to write C code such that it would be compiled with both GCC and G++. That is, the C subset of C++.
Wikipedia has a list with quite a few small incompatibilities, but it doesn't cover the case in this thread:

http://en.wikipedia.org/wiki/Compatibil ... nd_C%2B%2B