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

deadalnix via Digitalmars-d digitalmars-d at puremagic.com
Fri May 15 11:37:14 PDT 2015


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. What most malloc() implementations 
> practically do in their first line is:
>
> if (size == 0) size = 1;
>
> and take it from there.
>

There are actually 2 way to do this in malloc. Either you return 
null, but then you need to be able to accept null in the free 
implementation (as malloc must return a freeable pointer) or you 
just bump to 1.

Both are valid per spec and there are implementations of malloc 
for both.


More information about the Digitalmars-d mailing list