C compilers and range checking
Moderator: Ras
-
- Posts: 1494
- Joined: Thu Mar 30, 2006 2:08 pm
C compilers and range checking
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.
Re: C compilers and range checking
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.
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.

-
- Posts: 778
- Joined: Sat Jul 01, 2006 7:11 am
Re: C compilers and range checking
You could try this though it looks like you would have to recompile gcc.
http://sourceforge.net/projects/boundschecking/
http://sourceforge.net/projects/boundschecking/
-
- Posts: 1260
- Joined: Sat Dec 13, 2008 7:00 pm
Re: C compilers and range checking
std::tr1::array has it, but that's C++ 

-
- Posts: 20943
- Joined: Mon Feb 27, 2006 7:30 pm
- Location: Birmingham, AL
Re: C compilers and range checking
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.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.
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...
-
- Posts: 12792
- Joined: Wed Mar 08, 2006 8:57 pm
- Location: Redmond, WA USA
Re: C compilers and range checking
Try this thing: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.
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.
-
- Posts: 1808
- Joined: Wed Mar 08, 2006 9:19 pm
- Location: Oslo, Norway
Re: C compilers and range checking
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: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.
Code: Select all
inline Bitboard file_bb(File f) {
return FileBB[f];
}
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));
}
-
- Posts: 1822
- Joined: Thu Mar 09, 2006 11:54 pm
- Location: The Netherlands
Re: C compilers and range checking
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).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.
Valgrind is quite amateuristic software.
Finding bugs in systematic manner is simply not important for products.
Selling them is.