Second Draft: Placement New Expression

Richard (Rikki) Andrew Cattermole richard at cattermole.co.nz
Tue Nov 19 21:18:53 UTC 2024


On 20/11/2024 8:08 AM, Paul Backus wrote:
> On Tuesday, 19 November 2024 at 05:17:20 UTC, Richard (Rikki) Andrew 
> Cattermole wrote:
>> On 19/11/2024 6:05 PM, Richard (Rikki) Andrew Cattermole wrote:
>>> Why not support calling the allocator with the size needed?
>>>
>>> ```d
>>> struct Allocator {
>>>      void[] allocate(size_t);
>>> }
>>>
>>> Allocator a;
>>>
>>> new (allocator) S;
>>> new (malloc) S;
>>> ```
>>>
>>> DONE.
>>
>> There are three reasons that motivate me for allocator support:
>>
>> 1. It would be @safe.
> 
> It would not be @safe, because there is no way the compiler can be sure 
> that the void[] returned from allocate will not be aliased by other 
> pointers/references/slices. The programmer would have to verify that for 
> each individual allocator implementation, and use @trusted once they had 
> done so.

If there is anything like that is occurring outside of an allocator, or 
a misbehaving allocator, what expression you used to call an allocator 
is irrelevant, you have significantly bigger problems to attend to.

At the very least you will have program corruption, at best a crash.

I've experienced and debugged this plenty, without the need for 
placement new.



More information about the dip.development mailing list