[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Scripting

Dan Helfman wrote:

> Everyone seems to be very polarized in this debate between slow
> scripting languages with all kinds of safety checks and speedy compiled
> languages with no checks at all. Perhaps that's because most languages
> available today fall easily into one or the other of these two
> categories. But what about a hypothetical language that is natively
> compiled for speed, but still does some safety checks at compile-time
> and possibly even at run-time.

There *are* plenty of those - Ada, Pascal, Modula - to name but three.

B (from which C evolved) was somewhat revolutionary in *NOT* checking
array bounds.

However, it's quite easy to add array bound checking to C++ if you'd
like to have it. You can write a new class called 'array' containing
the data for the array and it's length - then overload 'operator[]'
and 'operator=' with inlined functions that perform the necessary

Then you can declare:

  class aInt ; /* Array of int */

  aInt x ( 10 ) ;   /* Which is ugly */

...and once you've done that, the syntax for accessing the
array is very natural:

  x[5] = 1234 + x[0] ;   /* Just like a regular array */

You could then get fancy and attempt to perform error recovery or
produce a nice graphical error message instead of just crashing.
You could even do things like saving the current game before
giving up and exiting.

You can also overload operator& to prevent people taking
the address of one of your array elements and
thereby bypassing the checking.

This is neat because you can just choose which arrays have bounds
checking and which don't and also build 'trusted' operations into
the array class that don't have to be bound checked.

Personally, I wouldn't find array bound checking very useful because
most of the time I'm using dynamically allocated storage, mmap'ed
files, pointers to shared memory, multiple threads, etc - hardly
ever something as simple as a standard array.

----------------------------- Steve Baker -------------------------------
Mail : <sjbaker1@airmail.net>   WorkMail: <sjbaker@link.com>
URLs : http://www.sjbaker.org
       http://plib.sf.net http://tuxaqfh.sf.net http://tuxkart.sf.net
       http://prettypoly.sf.net http://freeglut.sf.net
       http://toobular.sf.net   http://lodestone.sf.net