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

via Digitalmars-d digitalmars-d at puremagic.com
Fri May 15 23:59:33 PDT 2015


On Saturday, 16 May 2015 at 02:05:22 UTC, Andrei Alexandrescu 
wrote:
>> 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

You should, C is just not a language designed for type safety and 
correctness.


More information about the Digitalmars-d mailing list