Computer Chess Club Archives


Search

Terms

Messages

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

Author: Matt Taylor

Date: 02:42:05 01/22/03

Go up one level in this thread


On January 21, 2003 at 12:26:06, Russell Reagan wrote:

>To add debugging code you can use the preprocessors #if defined #ifdef #ifndef
>#elif #else to include certain lines depending on whether you are compiling in
>"debug" mode or in "release" mode. You can also add assert()'s. When you run in
>debug mode it will be slower, but you are only checking for bugs then so speed
>is not important. When you run in release mode, all of the debugging code goes
>away automagically. So if you have a function:
>
>int array[64];
>
>__ineline int f(i) {
>    assert(i < 64);
>    return array[64];
>}
>
>The function should not be any slower than just using the array directly. The
>difference is that when you compile in debug mode, the assert goes away (at
>least the compiler doesn't see it). You can write your own assert macro if you
>want, something like this:
>
>int error (char * exp, char * file, char * line) {
>    printf("error: %s %s %s\n", exp, file, line);
>}
>
>#ifdef DEBUG
>#define assert(exp) (void(0))
>#else
>#define assert(exp) (void)( (exp) || (error("exp", __FILE__, __LINE__)) )
>#endif
>
>Then if you want to compile your program in "debug" mode, just add this:
>
>#define DEBUG
>
>If you want to compile for speed, just remove that line, or do:
>
>#undef DEBUG
>
>I don't see much point in bounds checking, unless you are going to write code to
>dynamically allocate a new array and copy over the entire contents of the old
>array on the fly. If you were going to do that, you might as well use C++ and
>use an std::vector.

Omitted debug code through the preprocessor is a pretty handy trick. The only
problem is that you have to actually -trip- the fault to find it. It is a good
(free) tradeoff, but it won't catch even half of what tools can find. Plus we
all know that, as developers, who really -wants- to sit around for hours testing
code? ;)

-Matt



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.