www.digitalmars.com         C & C++   DMDScript  

D - array bounds checking question.

reply "chris jones" <flak clara.co.uk> writes:
I have read that it is posible to turn it off but is this globaly or can you
do it for specific blocks of code as you can in Delphi? I like to leave it
on for the most part and just turn it off in specific places where a
significant speed benefit is to be had.

thanks,

chris
Sep 10 2002
parent reply Burton Radons <loth users.sourceforge.net> writes:
chris jones wrote:

 I have read that it is posible to turn it off but is this globaly or can you
 do it for specific blocks of code as you can in Delphi? I like to leave it
 on for the most part and just turn it off in specific places where a
 significant speed benefit is to be had.
In those situations you'll want to use the two-register loop as well, so the method kills two birds: int [] x = new int [45]; for (int *c = x, end = c + x.length; c < end; c ++) ... Voila, no bounds checking. If you want to index without the bounds check, you can cast: ((int *) x) [l] There's no way to turn it off for a block of code.
Sep 10 2002
next sibling parent reply "Sandor Hojtsy" <hojtsy index.hu> writes:
"Burton Radons" <loth users.sourceforge.net> wrote in message
news:3D7E80A3.9010608 users.sourceforge.net...
 chris jones wrote:

 I have read that it is posible to turn it off but is this globaly or can
you
 do it for specific blocks of code as you can in Delphi? I like to leave
it
 on for the most part and just turn it off in specific places where a
 significant speed benefit is to be had.
In those situations you'll want to use the two-register loop as well, so the method kills two birds: int [] x = new int [45]; for (int *c = x, end = c + x.length; c < end; c ++) ...
Hmm. Seems quite an automatic task. Maybe the optimizing compiler could generate similar code if you don't tinker with the array length inside a simple loop. A single bound check before the loop would do.
 Voila, no bounds checking.  If you want to index without the bounds
 check, you can cast:

      ((int *) x) [l]
What if we invent a new property: int a = x.unchecked(1);
 There's no way to turn it off for a block of code.
That's OK.
Sep 11 2002
parent reply "chris jones" <flak clara.co.uk> writes:
"Sandor Hojtsy" <hojtsy index.hu> wrote in message
news:almqrr$1tv7$1 digitaldaemon.com...
 "Burton Radons" <loth users.sourceforge.net> wrote in message
 news:3D7E80A3.9010608 users.sourceforge.net...
 chris jones wrote:

 I have read that it is posible to turn it off but is this globaly or
can
 you
 do it for specific blocks of code as you can in Delphi? I like to
leave
 it
 on for the most part and just turn it off in specific places where a
 significant speed benefit is to be had.
In those situations you'll want to use the two-register loop as well, so the method kills two birds: int [] x = new int [45]; for (int *c = x, end = c + x.length; c < end; c ++) ...
Hmm. Seems quite an automatic task. Maybe the optimizing compiler could generate similar code if you don't tinker with the array length inside a simple loop. A single bound check before the loop would do.
 Voila, no bounds checking.  If you want to index without the bounds
 check, you can cast:

      ((int *) x) [l]
What if we invent a new property: int a = x.unchecked(1);
I like the idea, mabey 'unsafe' would be a better keyword, it dicourages use for people who mabey dont know whaht they are doing. Plus there should be a global switch to force bounds checking to aid debuging so you dont have to alter the source code. chris
Sep 11 2002
parent reply Pavel Minayev <evilone omen.ru> writes:
chris jones wrote:

 for people who mabey dont know whaht they are doing. Plus there should be a
 global switch to force bounds checking to aid debuging so you dont have to
 alter the source code.
Array bounds checking is on in debug builds by default.
Sep 11 2002
parent "chris jones" <flak clara.co.uk> writes:
"Pavel Minayev" <evilone omen.ru> wrote in message
news:alo40g$1abb$1 digitaldaemon.com...
 chris jones wrote:

 for people who mabey dont know whaht they are doing. Plus there should
be a
 global switch to force bounds checking to aid debuging so you dont have
to
 alter the source code.
Array bounds checking is on in debug builds by default.
Yes but i mean if it did become posible to have an option to turn bounds checking of for certain pieces of code, it would also be desirable to be able to force them to bounds check in debug builds. As it is its all or nothing. I still want bound checking in release builds but with the option of turning it off for certain bits of code, but still be able to overide that globaly for debug builds. chris
Sep 11 2002
prev sibling parent "Sean L. Palmer" <seanpalmer earthlink.net> writes:
This is making me want language support for iterators.

Pointers are just a wee bit more dangerous of a thing than you need in this
case.  And the two register loop is commonplace enough it deserves
representation or at least syntax sugar.

Sean

"Burton Radons" <loth users.sourceforge.net> wrote in message
news:3D7E80A3.9010608 users.sourceforge.net...
 chris jones wrote:

 I have read that it is posible to turn it off but is this globaly or can
you
 do it for specific blocks of code as you can in Delphi? I like to leave
it
 on for the most part and just turn it off in specific places where a
 significant speed benefit is to be had.
In those situations you'll want to use the two-register loop as well, so the method kills two birds: int [] x = new int [45]; for (int *c = x, end = c + x.length; c < end; c ++) ... Voila, no bounds checking. If you want to index without the bounds check, you can cast: ((int *) x) [l] There's no way to turn it off for a block of code.
Sep 11 2002