Re: C++ Bounds Checking



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).

--
.



Relevant Pages

  • Re: C++ Bounds Checking
    ... of the array itself. ... There are as many ways to do this as there are programmers, ... elements that are used only so the subroutine has a full array. ... I tried to use the intrinsic sum but got a buffer overflow at runtime. ...
    (comp.lang.fortran)
  • Re: C++ Bounds Checking
    ... of the array itself. ... There are as many ways to do this as there are programmers, ... I tried to use the intrinsic sum but got a buffer overflow at runtime. ... The best of the runtime diagnostics is Salford/Silverfrost who do ...
    (comp.lang.fortran)
  • Re: Not enough parallelism in programming
    ... Tools can provide feedback to programmers on deductions that can be made from their individual expressions, to tell them some things about what they are really saying, especially when their overall expression appears to be unproductive. ... It should analyse the complexity in terms of basic ... Looking up an array element in a not-required-to-be-contiguous array ... analyses are likely to be about as successful as any other analyses ...
    (comp.arch)
  • Re: Static vs. Dynamic typing (big advantage or not)---WAS: c.programming: OOP and memory management
    ... >>Sure, the vector class is useful, and I use it most of the time. ... > to the language standard. ... Because a variable-size array type is a wheel that C++ programmers have ...
    (comp.object)
  • Re: Static vs. Dynamic typing (big advantage or not)---WAS: c.programming: OOP and memory management
    ... >>Sure, the vector class is useful, and I use it most of the time. ... > to the language standard. ... Because a variable-size array type is a wheel that C++ programmers have ...
    (comp.programming)