std.allocator.allocate(0) -> return null or std.allocator.allocate(1)?

Andrei Alexandrescu via Digitalmars-d digitalmars-d at puremagic.com
Mon May 18 08:16:38 PDT 2015


On 5/18/15 5:16 AM, Peter Alexander wrote:
> On Sunday, 17 May 2015 at 20:31:50 UTC, deadalnix wrote:
>> On Sunday, 17 May 2015 at 14:13:03 UTC, Peter Alexander wrote:
>>> On Friday, 15 May 2015 at 16:36:29 UTC, Andrei Alexandrescu wrote:
>>>> This is a matter with some history behind it. In C, malloc(0) always
>>>> returns a new, legit pointer that can be subsequently reallocated,
>>>> freed etc.
>>>
>>> Is the invariant malloc(0) != malloc(0) the only thing that makes 0 a
>>> special case here?
>>
>> Doesn't need to be, the spec only say it must be passable to free.
>
> So here's my question: can we just make allocate(0) do nothing special?
> i.e. allocate a non-null, but still 0 length buffer?

Yes, allocation could be rounded to whatever minimum the allocator 
supports, then a zero-length slice be taken of the result. -- Andrei



More information about the Digitalmars-d mailing list