Is PureMalloc actually... pure?

Chris Katko ckatko at gmail.com
Sat Jan 11 19:05:48 UTC 2020


Pure implies (*requires) no side effects, right?

>UNIX 98 requires that errno be set to ENOMEM upon failure. 
>Purity is achieved by saving and restoring the value of errno, 
>thus behaving as if it were never changed.

Saving and restoring a global variable is is __not__ the same as 
no side effects. without a mutex, two threads could easily create 
a date race.

void* pureMalloc()(size_t size) @trusted pure @nogc nothrow
{
     const errnosave = fakePureErrno;
     void* ret = fakePureMalloc(size);
     fakePureErrno = errnosave;
     return ret;
}

I mean, on a practical level, malloc almost never fails so this 
probably hasn't shown up as a bug in 99% of code.


More information about the Digitalmars-d mailing list