Bug 6305 – String literals don't always have a 0 appended to them
Status
RESOLVED
Resolution
INVALID
Severity
minor
Priority
P2
Component
dmd
Product
D
Version
D1 (retired)
Platform
Other
OS
Windows
Creation time
2011-07-13T01:57:00Z
Last change time
2011-07-13T12:22:48Z
Assigned to
nobody
Creator
verylonglogin.reg
Comments
Comment #0 by verylonglogin.reg — 2011-07-13T01:57:25Z
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.
Comment #1 by schveiguy — 2011-07-13T05:35:41Z
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
Comment #2 by verylonglogin.reg — 2011-07-13T05:40:27Z
(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.
Comment #3 by schveiguy — 2011-07-13T05:58:14Z
Oops, sorry for the noise.
Comment #4 by schveiguy — 2011-07-13T12:22:48Z
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.