Computer Chess Club Archives


Search

Terms

Messages

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

Author: Scott Gasch

Date: 13:02:25 01/21/03

Go up one level in this thread


On January 21, 2003 at 15:00:41, Daniel Clausen wrote:

>(a)
>Using the right type not only makes the application less buggy, it also makes it
>more readable in the sense, the the interface is clearer.

Yes I understand... I definitely recognize my bad habit and am trying to break
it.  My new code is all good and my old code gets fixed when I find a problem.
I just mentioned this now as a preface to my point about ASSERTs before
dereferencing an array element.

>Additionally, the const-qualifier in the parameter list makes it clear, that the
>function won't change anything within the Board structure. (or class) Again,
>this not only makes the whole thing a bit safer, but also clearer. (especially
>when the name of the method is not that clear - which is a fault in itself
>though ;)

The const operator is ok but some people fail to understand that memory tagged
const is not protected from buggy code that mistakenly changes it as long as the
change is not overt.  Const allows the compiler to say "no" when you try to
directly or indirectly change something you shouldn't.  But it does not save you
from mistakenly corrupting the const struct with buggy code that, for example,
references an array out of bounds.  Any compiler I have ever seen does not place
const things in read only memory pages.  I kinda wish they would... for example
put things like this:

const int g_iMobilityBonus[15] = { whatever };

in it's own section in the EXE (not the traditional data / code section) and
tell the loader to map that section into memory and protect the pages as read
only.  I don't think this is done currently.

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.