Thread overview
[Issue 6305] New: String literals doesn't already have a 0 appended to them
Jul 13, 2011
Denis
[Issue 6305] String literals don't always have a 0 appended to them
Jul 13, 2011
Denis
July 13, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=6305

           Summary: String literals doesn't already have a 0 appended to
                    them
           Product: D
           Version: D1
          Platform: Other
        OS/Version: Windows
            Status: NEW
          Severity: minor
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: verylonglogin.reg@gmail.com


--- Comment #0 from Denis <verylonglogin.reg@gmail.com> 2011-07-13 01:57:25 PDT ---
According to http://www.digitalmars.com/d/1.0/arrays.html#printf
"String literals already have a 0 appended to them"
But if we create static arrays, they are exactly one after another:

import std.stdio;
const s1 = "abcd", s2 = "EFG", s3 = "h";
void main() {
    writefln("%s %s %s\n%s %s %s",
        s1, *(s1.ptr + s1.length), cast(int) *(s1.ptr + s1.length),
        s2, *(s2.ptr + s2.length), cast(int) *(s2.ptr + s2.length)
    );
}

Prints:
abcd E 69
EFG h 104

If I missed something, I think this "something" should be mentioned in documentation near citation above.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
July 13, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=6305


Steven Schveighoffer <schveiguy@yahoo.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |schveiguy@yahoo.com


--- Comment #1 from Steven Schveighoffer <schveiguy@yahoo.com> 2011-07-13 05:35:41 PDT ---
This seems to be windows-specific.  The exact code produces this output on linux (as far back as 2.033, the earliest installed compiler I have):

abcd  0
EFG  0

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
July 13, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=6305



--- Comment #2 from Denis <verylonglogin.reg@gmail.com> 2011-07-13 05:40:27 PDT ---
(In reply to comment #1)
> This seems to be windows-specific.  The exact code produces this output on linux (as far back as 2.033, the earliest installed compiler I have):
> 
> abcd  0
> EFG  0

It is a D1 only issue.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
July 13, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=6305


Steven Schveighoffer <schveiguy@yahoo.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|schveiguy@yahoo.com         |


--- Comment #3 from Steven Schveighoffer <schveiguy@yahoo.com> 2011-07-13 05:58:14 PDT ---
Oops, sorry for the noise.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
July 13, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=6305


Steven Schveighoffer <schveiguy@yahoo.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |schveiguy@yahoo.com
         Resolution|                            |INVALID


--- Comment #4 from Steven Schveighoffer <schveiguy@yahoo.com> 2011-07-13 12:22:48 PDT ---
Actually, I did have a D1 compiler laying around.  And I figured out the issue.

The issue is D1's type inference treats string literal types as char[N] where N is a uint.  Note that D2's type inference treats string literals as immutable(char)[].  So the issue is that you are not declaring a type, and D is assuming you meant it to be a fixed-sized array.  So the literal *does* have a zero, but it is copied into your declared fixed-sized arrays in the global segment without the zero.

I figured it out by doing:

pragma(msg, typeof(s1).stringof);

which prints:
char[4u];

If you do this:

const string s1 = "abcd", s2 = "EFG", s3 = "h";

Then it works as you expect.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------