What is the Correct way to Malloc in @nogc section?

Jakob Ovrum via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Feb 12 21:56:35 PST 2015


On Thursday, 12 February 2015 at 23:27:51 UTC, Kitt wrote:
> The Exception obviously uses the GC, and would need to be 
> replaced with a printf or something; however, emplace doesn't 
> have a @nogc replacement that I know of. What I'd like to know, 
> from people much more knowledgeable about the ins and outs of 
> D, is what the "Best" or "Correct" way to allocate and 
> deallocate within @nogc sections?

This issue, as well as

> PS: Tangentially related, what hashing algorithm does D use? 
> I've seen people suggest using typeid(object).toHash(&object); 
> however, toHash isn't @nogc, so I need to write my own Hashing 
> function. For the sake of consistency and peace of mind, I'd 
> really like to match mine as closely to Ds internal one as 
> possible.

this one, are related to the loss of guarantee attributes when 
using TypeInfo methods, most recently discussed in this 
thread[1]. It is essentially a bug, and a work in progress.

The correct way to handle OOM in D is to call 
core.exception.onOutOfMemoryError:

---
if (auto p = malloc(...))
     /* use p */
else
     onOutOfMemoryError(); // Throws OutOfMemoryError without 
allocating memory
---

[1] 
http://forum.dlang.org/post/krccldftrbbczahasvbi@forum.dlang.org


More information about the Digitalmars-d-learn mailing list