GC.malloc is pure - wat

ag0aep6g via Digitalmars-d digitalmars-d at puremagic.com
Thu Mar 31 22:00:43 PDT 2016


On 01.04.2016 00:52, deadalnix wrote:
> 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.

How does malloc expose its state and GC.malloc doesn't?

* They have the same signature. Both return pointers (to mutable data).
* malloc has free, GC.malloc has GC.free. So you can manually free the 
memory in both cases.
* You don't have to free memory from malloc. You can let it leak.

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

This also happens with functions that have an implementation in source:

----
int[] f() pure nothrow {return [0];}

void main()
{
     auto a = f();
     auto b = f();
     a[0] = 1;
     b[0] = 2;

     import std.stdio;
     writeln(a is b); /* should be false */
     writeln(a[0]); /* should be 1 */
}
----

Prints "true" and "2" when compiled with `dmd -O -release`.

So I don't think that lying to the compiler is the problem here.


More information about the Digitalmars-d mailing list