Pointers - Is it safe to point to invalid memory?
monkyyy
crazymonkyyy at gmail.com
Sat Aug 16 22:39:08 UTC 2025
On Saturday, 16 August 2025 at 22:28:15 UTC, Andy Valencia 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?
>
> Andy
c++ iterators had to *start* as backwards compadable with plain
pointers (steponov was a no body before stl, and even if the
standards care about him now, they wouldntve before he published)
this was terrible, that api is ugly
Iterators are two pointers and one is 1 off the end of the
array(presumably so you could make the ugly for statement work);
I think its a little culty to claim the "c's biggest mistake" but
as an api, yes its better in ranges
d is c++ replacement, and ranges are about cleaning up iterator,
d2 and ranges was 10-ish years ago, programming in d is also
10-ish years ago
More information about the Digitalmars-d-learn
mailing list