Re: C++ Bounds Checking



On 2007-06-29 17:17:23 -0300, "Wade Ward" <invalid@xxxxxxxxxxxx> said:


"dpb" <none@xxxxxxx> wrote in message news:f632dr$56g$1@xxxxxxxxxxx
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).
So bounds checking is going to issue a diagnostic if I want to assign to say
array(-1) or array(90) given that I have an 85-dimensional entity. Indeed,
I tried to use the intrinsic sum but got a buffer overflow at runtime. I
try to familiarize myself with the intrinsics as I go. It just seems like a
tall order to ask a compiler to know what's going to happen at runtime.

The runtime system is there to catch things at, well er, runtime.
The best of the runtime diagnostics is Salford/Silverfrost who do
complete undefined variable checking. They also have a matching C++
with the same level of diagnostics. Several other Fortrans deserve
honorable mentions or better. Almost all have subscript checking
but undefined variable checking is not coomon. Some try by offering
to initialize variables to NaNs.




.



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
    ... intrinsic sum, what would that 85th integer be? ... Bounds checking finds cases where the array is addressed _outside_ of the defined bounds of the array itself. ... There are as many ways to do this as there are programmers, ... A couple of obvious ways in your case would be to put the computationusing 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. ...
    (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)