C compilers and range checking

Discussion of chess software programming and technical issues.

Moderator: Ras

mjlef
Posts: 1494
Joined: Thu Mar 30, 2006 2:08 pm

C compilers and range checking

Post by mjlef »

Sorry if this is such a dumb, basic question, but do any current C compilers have array range (bounds) checking? I cannot seem to find this, which was certainly a standard feature of older Pascal compilers.
gingell

Re: C compilers and range checking

Post by gingell »

If you are on Linux you can try the tool Valgrind.

In general bounds checking is very hard to implement for C because most of the time the language doesn't have a notion of array bounds. If you pass a char * to a function, there's no way the compiler can tell how much space that pointer refers to.

In languages like Pascal and Ada, you array bounds are known either statically or at runtime. It's very easy then for the compiler to generate warnings when you compile or insert range checks at run time.

I thought about writing my engine in Ada, but I don't suppose I'd get very many third party builds if I did that. :)
jwes
Posts: 778
Joined: Sat Jul 01, 2006 7:11 am

Re: C compilers and range checking

Post by jwes »

You could try this though it looks like you would have to recompile gcc.
http://sourceforge.net/projects/boundschecking/
Gian-Carlo Pascutto
Posts: 1260
Joined: Sat Dec 13, 2008 7:00 pm

Re: C compilers and range checking

Post by Gian-Carlo Pascutto »

std::tr1::array has it, but that's C++ :)
bob
Posts: 20943
Joined: Mon Feb 27, 2006 7:30 pm
Location: Birmingham, AL

Re: C compilers and range checking

Post by bob »

mjlef wrote:Sorry if this is such a dumb, basic question, but do any current C compilers have array range (bounds) checking? I cannot seem to find this, which was certainly a standard feature of older Pascal compilers.
There is also a package called "purify" that you can use. I used this several years ago, the only issue being that hash table pointers cause confusion. I solved this by just making the hash table a static array for the testing I did, to get rid of errors that were not really errors.

Seems like there was another package, but the name escapes me at the moment.

Another option is to patch gcc, as there is a "bounds-checking" set of patches you can apply. then you use -fbounds-checking when you want to compile with the extra bounds-checking code included. Leaving that out results in a normal executable...
Dann Corbit
Posts: 12792
Joined: Wed Mar 08, 2006 8:57 pm
Location: Redmond, WA USA

Re: C compilers and range checking

Post by Dann Corbit »

mjlef wrote:Sorry if this is such a dumb, basic question, but do any current C compilers have array range (bounds) checking? I cannot seem to find this, which was certainly a standard feature of older Pascal compilers.
Try this thing:
http://sourceforge.net/projects/duma/
Also, Splint can perform static checking for many bounds errors:
http://www.splint.org/
Don't leave home without it.
Tord Romstad
Posts: 1808
Joined: Wed Mar 08, 2006 9:19 pm
Location: Oslo, Norway

Re: C compilers and range checking

Post by Tord Romstad »

mjlef wrote:Sorry if this is such a dumb, basic question, but do any current C compilers have array range (bounds) checking? I cannot seem to find this, which was certainly a standard feature of older Pascal compilers.
I usually avoid accessing arrays directly in high-level parts of the code. The actual array access is moved to tiny low-level inline functions. For instance, I have an array FileBB[8] which contains bitboards representing the eight files of the board. Instead of using FileBB[] all around my code, I call the following function:

Code: Select all

inline Bitboard file_bb(File f) {
  return FileBB[f];
}
Because this function only allows a value of type 'File' as input (and not a regular int), and I only call it from code that knows that the value passed to the function is a valid file (this is checked by assert() statements when running in debug mode), I know that there won't be any 'out of range' errors. In fact, it's even better than plain array range checking, because the compiler will give an error if I pass a value from the right range (0--7) but of an invalid type (like a rank instead of a file).

Moreover, by using function overloading, I can also pass a square instead of a file, and still get the right result:

Code: Select all

inline Bitboard file_bb(Square s) {
  return file_bb(square_file(s));
}
Strong typing is an extremely useful feature, and although the type system in C++ leaves a lot to be desired, you can avoid a lot of bugs by using it actively. In fact, the somewhat stronger typing is the most significant reason why I am using C++ rather than plain C.
diep
Posts: 1822
Joined: Thu Mar 09, 2006 11:54 pm
Location: The Netherlands

Re: C compilers and range checking

Post by diep »

mjlef wrote:Sorry if this is such a dumb, basic question, but do any current C compilers have array range (bounds) checking? I cannot seem to find this, which was certainly a standard feature of older Pascal compilers.
In C++ it's easy with a template (as we used for a big game). In C there is not much that's ok. I use today valgrind, but it doesn't find everything. Valgrind also has cachegrind by the way, very useful (though it isn't all 100% accurate as some manufacturers do not release their branch prediction model).

Valgrind is quite amateuristic software.

Finding bugs in systematic manner is simply not important for products.
Selling them is.