digitalmars.D.learn - "".dup is null
- Qian Xu (8/8) May 04 2009 Hi All,
- Steven Schveighoffer (6/13) May 04 2009 I think you might have a bug?
- Qian Xu (3/23) May 04 2009 They are not the same. s is null. "" is an empty string. empty string an...
- Qian Xu (10/16) May 04 2009 If I have not explained clearly.
- Steven Schveighoffer (15/31) May 04 2009 OK, your original post was this:
- Georg Wrede (5/52) May 04 2009 If I remember correctly, string literals are stored with a null
- Steven Schveighoffer (3/7) May 04 2009 Yes, that makes complete sense, thanks.
Hi All, The following code will throw an exception: char[] s; assert( s.dup is null); // OK assert("".dup !is null); // FAILED "".dup is expectly also an empty string. Is this a compiler bug? --Qian
May 04 2009
On Mon, 04 May 2009 09:46:57 -0400, Qian Xu <quian.xu stud.tu-ilmenau.de> wrote:Hi All, The following code will throw an exception: char[] s; assert( s.dup is null); // OK assert("".dup !is null); // FAILED "".dup is expectly also an empty string. Is this a compiler bug?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
May 04 2009
Steven Schveighoffer wrote:On Mon, 04 May 2009 09:46:57 -0400, Qian Xu <quian.xu stud.tu-ilmenau.de> wrote:They are not the same. s is null. "" is an empty string. empty string and null are definitely two thing.Hi All, The following code will throw an exception: char[] s; assert( s.dup is null); // OK assert("".dup !is null); // FAILED "".dup is expectly also an empty string. Is this a compiler bug?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
May 04 2009
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. -SteveIf 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.
May 04 2009
On Mon, 04 May 2009 10:22:49 -0400, Qian Xu <quian.xu stud.tu-ilmenau.de> wrote:Steven Schveighoffer wrote: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); 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... -SteveI think you might have a bug? "".dup is the same as s.dup, not sure why you would expect it to be not-null. -SteveIf 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.
May 04 2009
Steven Schveighoffer wrote:On Mon, 04 May 2009 10:22:49 -0400, Qian Xu <quian.xu stud.tu-ilmenau.de> wrote: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.Steven Schveighoffer wrote: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);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. -SteveIf 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.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
May 04 2009
On Mon, 04 May 2009 12:09:09 -0400, Georg Wrede <georg.wrede iki.fi> wrote: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.Yes, that makes complete sense, thanks. -Steve
May 04 2009