Empty Array is Null?

Frits van Bommel fvbommel at REMwOVExCAPSs.nl
Thu Mar 20 07:20:45 PDT 2008


Frits van Bommel wrote:
> BCS wrote:
>> Steven Schveighoffer wrote:
>>>
>>> Now, here is the weird part.  The compiler does some magic with 
>>> arrays.  If you are comparing an array with null, it changes the code 
>>> to actually just compare the array pointer to null.  So, the the 
>>> following code:
>>>
>>
>> no this is the weird part:
>>
>> IIRC this passes.
>>
>> char* cp = cast(char*)null;
>> char[] ca = ap[0..15];
>> assert(ca.ptr == null && ca.length == 15);
> 
> It does with DMD, if you s/ap/cp/, but I'm pretty sure what you're doing 
> is invoking undefined behavior. Or at least it should be, but I can't 
> seem to find any mention of it in the spec...

Oh wait, of course: pointer cast + using the pointer without casting it 
back first => undefined behavior.


More information about the Digitalmars-d-learn mailing list