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

Andrei Alexandrescu via Digitalmars-d digitalmars-d at puremagic.com
Fri May 15 19:05:21 PDT 2015


On 5/15/15 6:56 PM, Vladimir Panteleev wrote:
> On Friday, 15 May 2015 at 17:01:26 UTC, Andrei Alexandrescu wrote:
>> Since there's a long-established tradition that reallocate() and
>> free() accept a null pointer,
>
> Question, is there a strong rationale for this? The usefulness is
> obvious if malloc(0) returns null, but otherwise, attempting to free a
> resource that has never been created usually indicates a bug.

Yah, you don't want to special case null everywhere: cleanup functions, 
destructors, etc. etc. If deallocation wouldn't accept the null pointer, 
"if (p) free(p);" would cause carpal tunnel syndrome. -- Andrei



More information about the Digitalmars-d mailing list