@safe @nogc memory allocation

bearophile via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed May 28 14:09:24 PDT 2014


Dicebot:

> It is also because `malloc` can return null when out of memory 
> and `new` will throw an Error. Wrapper around `malloc` that 
> throws `OutOfMemoryError` on null can be considered of same 
> purity class as `new`.

One wrapper should have a template argument to specify the type 
of the items, to avoid the need of a cast. And instead of 
throwing an error it could also return a Nullable (the sizeof of 
such Nullable is the same as a pointer).

Nullable!(Titem*, null) talloc(Titem)(in size_t nItems);

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list