Placement New, Was: (Re: auto, var, raii,scope, banana)

Regan Heath regan at netwin.co.nz
Tue Jul 25 22:23:53 PDT 2006


On Wed, 26 Jul 2006 00:23:41 -0400, Chris Miller <chris at dprogramming.com>  
wrote:
> On Wed, 26 Jul 2006 00:13:53 -0400, Regan Heath <regan at netwin.co.nz>  
> wrote:
>> 1. 'new' implies heap allocation
>
> Except when it doesn't.  I'm talking about overloaded 'new'
>
> If Object`s were given 2 default new() implmentations, one that does the  
> current allocation, and another that allows you to specify a buffer  
> (also so that every Joe who wants this doesn't have to derive from the  
> class to provide it). When RAII is involved, it can choose which version  
> of new() to use (implementaion defined), using stack or not.

You're describing 'placement' new, yes?
http://www.informit.com/guides/content.asp?g=cplusplus&seqNum=160&rl=1

I dont think D has placement new, at least not a syntax for it like C++. I  
could be wrong.

You're right in that if D were to get placement new using the C++ syntax  
then the suggested RAII syntax (removing 'new') would make it impossible  
to declare an RAII class using placement new. But, it appears that  
placement new in D is done with a custom allocator and not a special  
syntax?

http://www.digitalmars.com/d/memory.html#newdelete

You coud write a custom allocator which would calculate offsets into  
pre-allocated memory returning that address for each allocation.  
Essentially placement new without the special syntax.

Has this topic come up before? What was Walters comment on it? I don't  
recall a discussion on it, I did a quick search and didn't turn up  
anything specific.

I wonder, does placement new in D have the same restrictions as in C++?  
i.e. cannot use stack memory, requires explicit 'delete'.

I wonder what constraints the RAII implementation in D puts on how objects  
are allocated. It's possible they can't be allocated on the stack .. I  
seem to recall Walter saying he'd like to go there, eventually, but I  
might be imagining things.

Regan



More information about the Digitalmars-d mailing list