Just a friendly reminder about using arrays in boolean conditions

Walter Bright newshound2 at digitalmars.com
Wed Nov 27 05:01:15 UTC 2024


On 11/25/2024 1:53 AM, Jonathan M Davis wrote:
> The core problem is that ptr is checked at all. Whether it's null or not is
> absolutely irrelevant to almost all D code.

It is relevant in the way I use it, as I will often recycle buffers to avoid the 
free/malloc dance. A non-null pointer tells me it is allocated.


> So, fundamentally, the check for null makes no sense even if it would have
> made sense with a C array, because a C array is just a naked pointer and has
> no language protections to ensure that you don't dereference it when it
> doesn't have elements. D arrays have those protections.

I recycle buffers in C code as well!

BTW, I understand that there can be confusion about what it means. In my own 
code I'm careful to use `buf.length`.



More information about the Digitalmars-d mailing list