Memory allocation purity

David Nadlinger via Digitalmars-d digitalmars-d at puremagic.com
Thu May 15 07:30:49 PDT 2014


On Thursday, 15 May 2014 at 06:50:06 UTC, Ola Fosheim Grøstad 
wrote:
> On Thursday, 15 May 2014 at 06:29:06 UTC, bearophile wrote:
>> A little example of D purity (this compiles):
>
>> bool randomBit() pure nothrow @safe {
>>    return (new int[1].ptr) > (new int[1].ptr);
>> }
>
> Yes, and then you may as well allow a random generator with 
> private globals. Because memoing is no longer sound anyway.

No, this particular example appears to be invalid code. Under the 
C memory model [1], the result of comparing two pointers to 
distinct objects (allocations) is undefined/unspecified behavior 
(I think it is undefined in C and unspecified in C++, but I don't 
remember the details).

Of course, you could rescue the example by casting the pointers 
to size_t or something along the lines, but this is something 
that could be disallowed in pure code.

David


[1] Which typically applies to D, unless defined otherwise. Our 
language specification is woefully incomplete in this regard, 
though.


More information about the Digitalmars-d mailing list