Re: C++ Bounds Checking
- From: dpb <none@xxxxxxx>
- Date: Fri, 29 Jun 2007 08:44:07 -0500
Wade Ward wrote:
....
How the heck are you supposed to get a compiler to get bounds right when programmers can't? In this source:....snip code example of using only subset of an array...
, I think I got the right answer by grace. If instead, I had used the intrinsic sum, what would that 85th integer be?
Depends on what you passed SUM() as the argument.
You're missing the point of bounds checking methinks. Bounds checking finds cases where the array is addressed _outside_ of the defined bounds of the array itself. In your example of an array of size 85, an attempt to address age(i) w/ i<1 or i>85 would trigger an error if bounds checking was in effect, otherwise the code would either silently address memory outside of the array (if it happened it was addressable space for your process) or _perhaps_ abort if it happened to be in system-protected memory. This is the classic "buffer overrun" problem.
Knowing that you have only used 84 of the 85 possible array entries is, indeed, your responsibility as programmer to ensure the code logic is written to "know" conditions such as that and handle them properly.
There are as many ways to do this as there are programmers, probably. A couple of obvious ways in your case would be to put the computation(s) using the array after it is filled w/ data in a subroutine and pass the elements that are used only so the subroutine has a full array. Or, an array slice could be passed to the SUM() intrinsic or the optional MASK argument could have been used if the array had been initialized to a specific value (something <0) would have been a good choice here as ages presumably aren't negative).
--
.
- Follow-Ups:
- Re: C++ Bounds Checking
- From: Wade Ward
- Re: C++ Bounds Checking
- From: dpb
- Re: C++ Bounds Checking
- References:
- C++ Bounds Checking
- From: Gary Scott
- Re: C++ Bounds Checking
- From: Gary Scott
- Re: C++ Bounds Checking
- From: Wade Ward
- C++ Bounds Checking
- Prev by Date: Re: Scientific Programming (journal) Dedicates Special Issue to Fortran
- Next by Date: Re: Please help me understand my code
- Previous by thread: Re: C++ Bounds Checking
- Next by thread: Re: C++ Bounds Checking
- Index(es):
Relevant Pages
|