Is there a GC'd malloc alternative?

Bill Baxter dnewsgroup at billbaxter.com
Mon Feb 19 13:09:13 PST 2007


0ffh wrote:
> 
> [Now, is this the right newsgroup? I am confused...]

Yep this is right.

> Hi, I'm new to D but got into it pretty quickly...
> it's heaven for a lazy old C coder! }:->>>
> 
> I just wonder if there is no library function that
> works like 'malloc' but in garbage collected memory?
> 
> Happy hacking, 0ffh
> 
> p.s.
> I am probably not looking for the 'new' operator,
> as 'new' seems to take only certain types as
> "argument", not the size of the requested memory
> chunk, as does 'malloc'.

You need to be careful with that kind of thing because the garbage 
collector needs to know whether each chunk of allocated memory could 
contain pointers or not.

If you allocate as void (as Johan recommended) the gc will assume that 
pointers *are* possible in that memory.  That's great if you plan on 
putting things with pointers in there, but if you're just going to use 
it for some kind of raw data then you'll be wasting time having the gc 
scan that memory.

If you allocate as ubyte, then the gc will assume there are no pointers 
there and won't scan that memory for outstanding references.

(note this behavior is only for DMD 1.001 and higher, DMD 1.0 was not 
type-aware and just scanned everything no matter what.)

--bb



More information about the Digitalmars-d mailing list