"".dup is null
Georg Wrede
georg.wrede at iki.fi
Mon May 4 09:09:09 PDT 2009
Steven Schveighoffer wrote:
> On Mon, 04 May 2009 10:22:49 -0400, Qian Xu
> <quian.xu at stud.tu-ilmenau.de> wrote:
>
>> Steven Schveighoffer wrote:
>>
>>> I think you might have a bug?
>>>
>>> "".dup is the same as s.dup, not sure why you would expect it to be
>>> not-null.
>>>
>>> -Steve
>>
>> If I have not explained clearly.
>> Here is the full code:
>>
>> char[] s;
>> assert(s is null);
>> assert(s.dup is null);
>>
>> assert("" !is null); // OK
>> assert("".dup !is null); // FAILED
>>
>> At least the last two lines behave not consistent.
>> Either both are failed, or both are passed.
>
>
> OK, your original post was this:
>
> assert( s.dup is null); // OK
> assert("".dup !is null); // FAILED
>
>
> The compiler always returns a null array if you dup an empty array. The
> reason being: why allocate memory for something that is zero length?
>
> If anything, the oddity is this line:
>
> assert("" !is null);
If I remember correctly, string literals are stored with a null
appended, so as to make them easier to use with OS calls, etc. Could it
be that "" stores a 1-byte string, consisting with just this null? Then
this behavior would be understandable.
> But of course, no memory is allocated for literals, so at least needless
> memory allocation does not occur.
>
> To be consistent, I think assert("" is null); should pass, but it's a
> minor inconsistency at best.
>
> I usually never compare arrays to null for this reason...
>
> -Steve
More information about the Digitalmars-d-learn
mailing list