Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: a question about the value of checking bounds

Author: Scott Gasch

Date: 11:18:05 01/21/03

Go up one level in this thread


One of the things I catch myself doing a lot in my code is using signed ints
where I should be using unsigned.  It's easier to type "int" than "unsigned" and
I'm lazy.  But I am trying to change!  Keep an eye out for this in your code.

One of the good coding habits I have developed is putting ASSERTS to check array
bounds in DEBUG builds.  For example:

ASSERT(uMobility < 15);
// note that if index is unsigned and the array zero based you need >= 0
ASSERT((uColor == WHITE) || (uColor == BLACK));
iScore[uColor] += g_iMobilityBonus[uMobility];

I also have a little function that computes a checksum "read-only" data
structures in my engine (like move generator tables, vector delta table, etc).
In debug builds I call this function at every node in the search tree. I found a
bug this way once, though it was a code generation bug and not mine.  My DEBUG
builds run at about 1knps, btw.

I also do a trick on PARANOID builds (which are ultra-DEBUG builds that search
about 20 nps) with VirtualProtect on Win32.  This function allows you to change
the protection of a page or range of pages in memory.  There is an equivalent in
POSIX but I don't recall it off the top of my head.  So after I allocate my
transposition, pawn hash, eval score etc... tables I lock them read only.  If
something tries to write them because of a wild array bound it will AV.  When I
want to update one I unlock the page I am going to update.  This is a big pain
in the ass and really slow... and I've never caught a bug this way.  But I like
having it in there anyway.

One thing I have thought about doing but never done is instead of putting my
move gen tables / vector table / delta table / hash key tables etc... in the
code segment of my EXE allocating them from the heap.  That way once I
initialize the data there at the start of the program I can change the
allocation to read only.

Oh, and once in a while I sneak my code in to work and run it on boundschecker
where we have a site license... I'm pretty sure I shouldn't do that so don't
tell my boss.

Scott



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.