On Sat, Aug 16, 2025 at 10:28:15PM +0000, Andy Valencia via Digitalmars-d-learn 
wrote:
> On Saturday, 16 August 2025 at 21:58:30 UTC, Paul Backus wrote:
> > Creating a pointer that points out-of-bounds does not, by itself,
> > result in undefined behavior.
> > 
> > However, such a pointer would not be considered a [safe value][1],
> > because dereferencing it *would* result in undefined behavior.
> 
> I'm just pondering whether the intention was to accomodate this
> looping pattern:
> 
> ```d
> int sum_values(int* p, uint nval) {
>   int result = 0;
>   foreach(_; 0 .. nval) {
>     result += *p++;
>   }
>   return result;
> }
> ```
> 
> A C idiom (I've so very much embraced not using pointers in my D
> world) which technically leaves "p" pointing beyond the memory range.
> Perhaps this is the special case being addressed?
[...]

Why does it need to be addressed?  D has arrays and slices that know
their own length.  From p and nval you can form the slice p[0 .. nval]
which lets you iterate safely without risking illegal memory accesses:

        int sum_values(int[] arr) {
                int result = 0;
                foreach (i; arr) {
                        result = i;
                }
                return result;
        }


T

-- 
Never wrestle a pig. You both get covered in mud, and the pig likes it.
              • ... Monkyyy via Digitalmars-d-learn
              • ... Monkyyy via Digitalmars-d-learn
              • ... Ali Çehreli via Digitalmars-d-learn
              • ... Richard (Rikki) Andrew Cattermole via Digitalmars-d-learn
              • ... Monkyyy via Digitalmars-d-learn
  • Re: Pointers - I... Monkyyy via Digitalmars-d-learn
  • Re: Pointers - I... Paul Backus via Digitalmars-d-learn
    • Re: Pointer... Andy Valencia via Digitalmars-d-learn
      • Re: Poi... Richard (Rikki) Andrew Cattermole via Digitalmars-d-learn
      • Re: Poi... monkyyy via Digitalmars-d-learn
      • Re: Poi... H. S. Teoh via Digitalmars-d-learn
      • Re: Poi... Paul Backus via Digitalmars-d-learn
        • Re:... monkyyy via Digitalmars-d-learn
        • Re:... Ali Çehreli via Digitalmars-d-learn

Reply via email to