[Issue 8185] Pure functions and pointers

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sun Jun 3 12:51:43 PDT 2012


http://d.puremagic.com/issues/show_bug.cgi?id=8185



--- Comment #13 from timon.gehr at gmx.ch 2012-06-03 12:53:36 PDT ---
(In reply to comment #12)
> (In reply to comment #11)
> > Pointers may only access their own memory blocks, therefore exactly those
> > blocks participate in argument value and return value.
> 
> What does 'their own memory block' mean?

The allocated memory block it points into.

> The problem is a pointer is basically an unbounded array,

That is wrong. The pointer is bounded, but it is generally impossible to devise
the exact bounds from the pointer alone. This is why D has dynamic arrays.

> and, if the access isn't restricted somehow, makes the
> function dependent on global memory state.

? A function independent of memory state is useless.

> 
> > But why does it even matter? Isn't this discussion mostly philosophical?
> 
> The compiler will happily assume that template functions are pure even when
> they clearly are not, and there isn't even a way to mark such functions as
> "impure" (w/o using hacks like calling dummy functions etc).
> Example - a function that is designed to operate on arrays, will always be
> called with a pointer to inside an array, and can assume that the previous and
> next element is always valid: 
> 
>   f4(T)(T* p) {
>       p[-1] += p[0];
>    }
> 
> The compiler thinks f4() is pure, when it clearly is not; optimizations based
> on that assumption are likely to result in corrupted data.

f4 _is_ 'pure' (it does not access non-immutable free variables). The compiler
is not allowed to perform optimizations that change defined program behavior.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list