Dynamic array comparison optimization: check for equal pointers?

Johan j at j.nl
Wed Jun 3 11:59:18 UTC 2020


On Wednesday, 3 June 2020 at 05:48:56 UTC, Patrick Schluter wrote:
> On Tuesday, 2 June 2020 at 19:38:04 UTC, Johan wrote:
>>
>> I am surprised that memcmp itself does not implement the 
>> pointers are equal check. Is there a big impact of adding the 
>> shortcircuit check to prevent scanning memory?
>
> memcmp() doesn't implement the check to avoid memcmp(NULL, 
> NULL, length) returning with 0 instead of segfaulting as it 
> does if any of the 2 pointers in NULL.
> It's a case of consistant behaviour, either you tolerate NULL 
> pointers in all cases or you segfault in all cases.

Interesting argument!
But "undefined behavior" does not mean the program has to 
segfault. It's perfectly fine to return 0 for the UB case.

-Johan



More information about the Digitalmars-d mailing list