Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: Bizar Question for programmers: very strange behaviour of my engine

Author: Dann Corbit

Date: 19:14:26 01/17/06

Go up one level in this thread


On January 17, 2006 at 14:57:02, Robert Hyatt wrote:

>On January 17, 2006 at 06:36:20, JW de Kort wrote:
>
>>On January 16, 2006 at 20:13:19, Robert Hyatt wrote:
>>
>>>On January 16, 2006 at 16:16:13, JW de Kort wrote:
>>>
>>>>Hi all,
>>>>
>>>>In my engine i want to use the following line:
>>>>
>>>>    !(bfZwart[kol+1]&bfBoven[rij])
>>>>
>>>>In only a few cases this lines crashes my engine. If i change the +1 to any
>>>>other value there is no problem!
>>>>
>>>>Does anybody know were i have to look for to solve this problem? I do not
>>>>understand how a comparison like the above can cause a program to crash.
>>>>
>>>>regards
>>>>Jan Willem
>>>
>>>also "&" is the bitwise AND operator I assume you understand?  So you are ANDing
>>>two values, and the result should be zero to satisfy that condition...
>>>
>>>How are bfZwart and bfBoven declared???
>>
>>That is correct.
>
>>
>>
>>unsigned char bfZwart [10]
>>bfBoven unsigned char [8]
>>
>>This code is part of the passer detection routine.
>
>
>I can't see any way it would crash so long as this is true:
>
>-1 <= kol < 9
>
>that way kol+1 will be between 0 and 9 and perfectly legal  Any other value and
>you are indexing out of the array on either end, which can be trouble.

Of course, the problem might show up there, but be generated elsewhere.
Since the data type of both arrays is unsigned char, the standard says that even
uninitialized data cannot cause a trap.

I suggest tests similar to the following:

#include <stdio.h>
#include <assert.h>

static unsigned char   bfZwart[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
static unsigned char   bfBoven[8] = {1, 2, 3, 4, 5, 6, 7, 8};

int             main(void)
{
    int             kol;
    int             rij;
    for (kol = -1; (kol + 1) < (sizeof bfZwart / sizeof bfZwart[0]); kol++) {
        assert(((kol + 1) >= 0) && ((kol + 1) < (sizeof bfZwart / sizeof
bfZwart[0])));
        for (rij = 0; rij < (sizeof bfBoven / sizeof bfBoven[0]); rij++) {
            assert((rij >= 0) && (rij < (sizeof bfBoven / sizeof bfBoven[0])));
            if (!(bfZwart[kol + 1] & bfBoven[rij]))
                puts("Yep.");
        }
    }
    return 0;
}



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.