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

Andrei Alexandrescu via Digitalmars-d digitalmars-d at puremagic.com
Sun May 17 09:22:33 PDT 2015


On 5/17/15 5:27 AM, Idan Arye wrote:
> On Sunday, 17 May 2015 at 12:09:37 UTC, Timon Gehr wrote:
>> On 05/17/2015 10:35 AM, Idan Arye wrote:
>>> ...
>>>
>>> How about defining `alloc.allocate(0)` to *be* an error?
>>
>> What is bad about allocate(0)?
>
> It's not that allocating 0 bytes is wrong by itself(at first glance it
> seemed very wrong to me, but then I remembered that you are never
> allowed to read-from or writ-to it more than 0 bytes), but it's how
> limited it's usefulness is compared to all the special-casing it
> requires, it might be better to simply disallow it.

Prolly not a good idea, 0 works well for the most part as a natural 
lower bound, e.g.:

void fun(size_t n)
{
    int[] a = theAllocator.makeArray!int(n);
    scope(exit) theAllocator.dispose(a);
    foreach (ref e; a)
    {
        ...
    }
    ....
}

Special casing code like this for n==0 (lest functions start throwing 
etc) is not a good strategy to condone.


Andrei



More information about the Digitalmars-d mailing list