GC.malloc is pure - wat

deadalnix via Digitalmars-d digitalmars-d at puremagic.com
Thu Mar 31 15:52:46 PDT 2016


On Thursday, 31 March 2016 at 22:18:03 UTC, ag0aep6g wrote:
> On 31.03.2016 23:25, Nordlöw wrote:
>> A solution is to fake purity through
>>
>> extern(C) pure nothrow @system @nogc
>> {
>>      void* malloc(size_t size);
>>      void* realloc(void* ptr, size_t size);
>>      void free(void* ptr);
>> }
>>
>> Pay attention to the use of malloc() though.
>>
>> Note that this makes malloc() strongly pure (its single 
>> parameters is
>> passed by value).
>
> As has been explained to me in this very thread, the fact that 
> malloc returns a pointer to mutable data makes it not strongly 
> pure.
>

Is it not pure, strong or weak. GC.malloc is pure because the 
memory is going to be garbage collected. malloc is not pure as 
the memory need to be freed. The state in the allocator is 
exposed to the program.

> Uh, as far as I see, that would be catastrophic.
>
> I feel like I must be missing something here again, but it 
> looks like dmd actually gets this wrong:
>
> ----
> extern(C) void* malloc(size_t size) pure nothrow;
>
> void main()
> {
>     auto a = cast(ubyte*) malloc(1);
>     auto b = cast(ubyte*) malloc(1);
>
>     import std.stdio;
>     writeln(a is b); /* should be false */
>     *a = 1;
>     *b = 2;
>     writeln(*a); /* should be 1 */
> }
> ----
>
> Compiled with dmd and no optimization switches, this prints 
> "false" and "1" as expected.
> Compiled with `dmd -O -release`, this prints "true" and "2". 
> The heck?
> With `ldc2 -O5 -release`: "false" and "1". No problem there.
>
> This looks like a serious bug in dmd to me.

Lie to the compiler and it will punish you for it.

DMD optimizes malloc because the only reason it would have to 
return a different pointer would be internal state (and in fact, 
it indeed has internal state).



More information about the Digitalmars-d mailing list