D - Should This Be Legal?
- Russ Lewis (12/12) Nov 01 2002 I was doing a search through an array, and I wanted to assert that I
- Burton Radons (4/13) Nov 01 2002 assert isn't an expression, so no. This assertion is redundant anyway -...
- Russ Lewis (15/28) Nov 01 2002 ACK!!!! I forgot the key thing in the whole code!
- Burton Radons (5/37) Nov 01 2002 I understood what you meant. It turns out assert is an expression - I
- Mike Wynn (22/29) Nov 01 2002 No its not legal and should never be.
I was doing a search through an array, and I wanted to assert that I
definitely would find what I was looking for in the array. So I coded
this, which crashed DLI 0.1.1:
for(int foo=0; assert(foo<array.length); foo++)
if(do_test(array[foo]))
do_stuff();
Is this be legal? If not, why not?
--
The Villagers are Online! http://villagersonline.com
.[ (the fox.(quick,brown)) jumped.over(the dog.lazy) ]
.[ (a version.of(English).(precise.more)) is(possible) ]
?[ you want.to(help(develop(it))) ]
Nov 01 2002
Russ Lewis wrote:
I was doing a search through an array, and I wanted to assert that I
definitely would find what I was looking for in the array. So I coded
this, which crashed DLI 0.1.1:
for(int foo=0; assert(foo<array.length); foo++)
if(do_test(array[foo]))
do_stuff();
Is this be legal? If not, why not?
assert isn't an expression, so no. This assertion is redundant anyway -
in normal builds the array[foo] will do a bounds check, in release
builds neither will be checked.
Nov 01 2002
Burton Radons wrote:Russ Lewis wrote:ACK!!!! I forgot the key thing in the whole code! There was supposed to be a break if you match the test: for(int foo=0; assert(foo<array.length); foo++) if(do_test(array[foo])) { do_stuff(); break; } Does that make more sense now? -- The Villagers are Online! http://villagersonline.com .[ (the fox.(quick,brown)) jumped.over(the dog.lazy) ] .[ (a version.of(English).(precise.more)) is(possible) ] ?[ you want.to(help(develop(it))) ]I was doing a search through an array, and I wanted to assert that I definitely would find what I was looking for in the array. So I coded this, which crashed DLI 0.1.1: for(int foo=0; assert(foo<array.length); foo++) if(do_test(array[foo])) do_stuff(); Is this be legal? If not, why not?assert isn't an expression, so no. This assertion is redundant anyway - in normal builds the array[foo] will do a bounds check, in release builds neither will be checked.
Nov 01 2002
Russ Lewis wrote:Burton Radons wrote:I understood what you meant. It turns out assert is an expression - I assume to allow chaining with comma - but it's still incorrect here as it returns void. I throw an error on the occasion now. I don't think it's a common enough form to have it in the language.Russ Lewis wrote:ACK!!!! I forgot the key thing in the whole code! There was supposed to be a break if you match the test: for(int foo=0; assert(foo<array.length); foo++) if(do_test(array[foo])) { do_stuff(); break; } Does that make more sense now?I was doing a search through an array, and I wanted to assert that I definitely would find what I was looking for in the array. So I coded this, which crashed DLI 0.1.1: for(int foo=0; assert(foo<array.length); foo++) if(do_test(array[foo])) do_stuff(); Is this be legal? If not, why not?assert isn't an expression, so no. This assertion is redundant anyway - in normal builds the array[foo] will do a bounds check, in release builds neither will be checked.
Nov 01 2002
"Russ Lewis" <spamhole-2001-07-16 deming-os.org> wrote in message
news:3DC2903C.660DA40C deming-os.org...
I was doing a search through an array, and I wanted to assert that I
definitely would find what I was looking for in the array. So I coded
this, which crashed DLI 0.1.1:
for(int foo=0; assert(foo<array.length); foo++)
if(do_test(array[foo]))
do_stuff();
Is this be legal? If not, why not?
No its not legal and should never be.
apart from assert not being an expression
assert is a check for something that is NOT expected to happen and should
compile to nothing when non assert builds are built.
here quite clearly foo can be larger than array.length;
I assume your code should have been (where getting to the end would be an
error)
for(int foo=0; assert(foo<array.length); foo++)
if(do_test(array[foo]))
break;
arrays should be bounds checked in asserting builds (IMHO)
also asserts SHOULD not have side effects
the code
foo = 0;
do {
assert( ++foo < array.length );
count += array[foo];
} while( count < 100 );
should be a compiler warning (modification withing an assert)
Mike.
Nov 01 2002









Burton Radons <loth users.sourceforge.net> 