D - Bug in printf function?
- Bruno A. Costa (20/20) Apr 26 2004 Hi,
- J Anderson (7/19) Apr 26 2004 I don't know if this is a bug. I noticed this behaviour when not using
- Ant (6/29) Apr 26 2004 (I don't have D here)
- J Anderson (10/15) Apr 26 2004 You mean:
-
Stewart Gordon
(11/15)
Apr 26 2004
- J Anderson (6/10) Apr 26 2004 That works but as I explained before that causes a reallocation. D
- Vathix (7/17) Apr 26 2004 toStringz() only reallocates if there's not a zero byte past the end. D
- Bruno A. Costa (14/22) Apr 26 2004 If I do:
- Vathix (7/20) Apr 26 2004 D strings aren't always compatible with C strings. They often are, which...
-
Stewart Gordon
(18/24)
Apr 26 2004
- Walter (9/22) Apr 26 2004 sequence
Hi, The following code fails to produce the expected result: // ==================================== import std.string; int main (char[][] args) { byte bt = 0; printf (toString (bt)); return 0; } // ==================================== The printf function should display the string "0", but it shows the sequence "0123456789". The same thing occurs if I change the value of variable bt to any number less than 10. The problem *does not occur* if I concatenate the value returned from toString with other string: printf ("Value = " ~ toString(bt)); // Displays "Value = 0" correctly I am using dmd 0.82 and gcc 3.2.3 cheers. Bruno.
Apr 26 2004
Bruno A. Costa wrote:Hi, The following code fails to produce the expected result: // ==================================== import std.string; int main (char[][] args) { byte bt = 0; printf (toString (bt)); return 0; } // ====================================I don't know if this is a bug. I noticed this behaviour when not using printf as well. As a workaround (if it is a bug), you can go: printf (toString (bt).dup); -- -Anderson: http://badmama.com.au/~anderson/
Apr 26 2004
In article <c6ivk0$2roe$1 digitaldaemon.com>, J Anderson says...Bruno A. Costa wrote:(I don't have D here) will it print correctly with printf (toString (bt)~'\0'); // or "\0" ? AntHi, The following code fails to produce the expected result: // ==================================== import std.string; int main (char[][] args) { byte bt = 0; printf (toString (bt)); return 0; } // ====================================I don't know if this is a bug. I noticed this behaviour when not using printf as well. As a workaround (if it is a bug), you can go: printf (toString (bt).dup);
Apr 26 2004
Ant wrote:(I don't have D here) will it print correctly with printf (toString (bt)~'\0'); // or "\0" ? AntYou mean: printf (toString (bt) ~ "\0"); Yeah but: What that's doing is essentially a dup because ~ causes a reallocation of the array with a length. Afterwoods D automatically converts to a zero-terminated string any way. You could add " " on if you wished for the same effect. -- -Anderson: http://badmama.com.au/~anderson/
Apr 26 2004
Bruno A. Costa wrote:Hi, The following code fails to produce the expected result:<snip>printf (toString (bt));<snip> Does it work if you do the Right Thing by converting it to a null-terminated string? i.e. printf (toStringz(toString(bt))); Stewart. -- My e-mail is valid but not my primary mailbox, aside from its being the unfortunate victim of intensive mail-bombing at the moment. Please keep replies on the 'group where everyone may benefit.
Apr 26 2004
Stewart Gordon wrote:Does it work if you do the Right Thing by converting it to a null-terminated string? i.e. printf (toStringz(toString(bt))); Stewart.That works but as I explained before that causes a reallocation. D automaticly converts char [] to zero terminated string if you pass it into a c char * function. -- -Anderson: http://badmama.com.au/~anderson/
Apr 26 2004
On Mon, 26 Apr 2004 21:03:02 +0800, J Anderson <REMOVEanderson badmama.com.au> wrote:Stewart Gordon wrote:toStringz() only reallocates if there's not a zero byte past the end. D will implicitly convert char[] to char*, but whether or not it's zero terminated is up to the caller. This comes up too often ;) -- Christopher E. MillerDoes it work if you do the Right Thing by converting it to a null-terminated string? i.e. printf (toStringz(toString(bt))); Stewart.That works but as I explained before that causes a reallocation. D automaticly converts char [] to zero terminated string if you pass it into a c char * function.
Apr 26 2004
Stewart Gordon wrote:Does it work if you do the Right Thing by converting it to a null-terminated string? i.e. printf (toStringz(toString(bt))); Stewart.If I do: printf (toString (bt).dup); printf (toStringz(toString (bt))); it works. If I do: printf (toString(125)); // numbers >= 10 don't cause problems it works too, but if I pass a number < 10: printf (toString(5)); // Error: shows 56789 instead of 5. Note that the problem does not seem to be in toString function, because concatenated strings are displayed correctly: printf ("Value = " ~ toString (5)); // Displays "Value = 5" Cheers. Bruno.
Apr 26 2004
On Mon, 26 Apr 2004 10:41:13 -0300, Bruno A. Costa <bruno codata.com.br> wrote:If I do: printf (toString (bt).dup); printf (toStringz(toString (bt))); it works. If I do: printf (toString(125)); // numbers >= 10 don't cause problems it works too, but if I pass a number < 10: printf (toString(5)); // Error: shows 56789 instead of 5. Note that the problem does not seem to be in toString function, because concatenated strings are displayed correctly: printf ("Value = " ~ toString (5)); // Displays "Value = 5" Cheers. Bruno.D strings aren't always compatible with C strings. They often are, which makes it tricky. Why not use the D function stdout.printf() by importing std.stream and you won't have to worry about it. -- Christopher E. Miller
Apr 26 2004
Bruno A. Costa wrote: <snip>If I do: printf (toString(125)); // numbers >= 10 don't cause problems it works too, but if I pass a number < 10: printf (toString(5)); // Error: shows 56789 instead of 5.<snip> That gives insight into how toString works. Obviously it has a string "0123456789" somewhere, and in order to toString a single digit, it just returns a slice of the array. And the length gets lost in the conversion from char[] to char*. This is also consistent with my experience that slices invariably aren't null terminated. At least if they're not at the end of the string, and the string doesn't have an intervening null char in just the right place. OTOH, if the number >= 10, then it builds up the string rather than just cutting a slice. Which, with the way things are implemented, tends to lead to a null termination. Stewart. -- My e-mail is valid but not my primary mailbox, aside from its being the unfortunate victim of intensive mail-bombing at the moment. Please keep replies on the 'group where everyone may benefit.
Apr 26 2004
"Stewart Gordon" <smjg_1998 yahoo.com> wrote in message news:c6j62v$4np$1 digitaldaemon.com...That gives insight into how toString works. Obviously it has a string "0123456789" somewhere, and in order to toString a single digit, it just returns a slice of the array. And the length gets lost in the conversion from char[] to char*.You're on to me <g>. One advantage to garbage collection is you don't need to keep track of what is a malloc'd pointer and what is static, they can be mixed up. So, since most conversions are just a single digit, it's very fast to just slice a static array for them. This is one advantage to gc systems that few gc detractors recognize.
Apr 26 2004
"Walter" <newshound digitalmars.com> wrote in message news:c6jnbk$13o0$1 digitaldaemon.com..."Stewart Gordon" <smjg_1998 yahoo.com> wrote in message news:c6j62v$4np$1 digitaldaemon.com...Yes indeed. Someone needs to point that out in a book sometime, don't you think?That gives insight into how toString works. Obviously it has a string "0123456789" somewhere, and in order to toString a single digit, it just returns a slice of the array. And the length gets lost in the conversion from char[] to char*.You're on to me <g>. One advantage to garbage collection is you don't need to keep track of what is a malloc'd pointer and what is static, they can be mixed up. So, since most conversions are just a single digit, it's very fast to just slice a static array for them. This is one advantage to gc systems that few gc detractors recognize.
Apr 26 2004
"Matthew" <matthew.hat stlsoft.dot.org> wrote in message news:c6jrgb$1aln$1 digitaldaemon.com..."Walter" <newshound digitalmars.com> wrote in message news:c6jnbk$13o0$1 digitaldaemon.com...just"Stewart Gordon" <smjg_1998 yahoo.com> wrote in message news:c6j62v$4np$1 digitaldaemon.com...That gives insight into how toString works. Obviously it has a string "0123456789" somewhere, and in order to toString a single digit, itneedreturns a slice of the array. And the length gets lost in the conversion from char[] to char*.You're on to me <g>. One advantage to garbage collection is you don'tbeto keep track of what is a malloc'd pointer and what is static, they canfastmixed up. So, since most conversions are just a single digit, it's verysystemsto just slice a static array for them. This is one advantage to gcthink? LOL!that few gc detractors recognize.Yes indeed. Someone needs to point that out in a book sometime, don't you
Apr 26 2004
"Bruno A. Costa" <bruno codata.com.br> wrote in message news:c6iumk$2qar$1 digitaldaemon.com...Hi, The following code fails to produce the expected result: // ==================================== import std.string; int main (char[][] args) { byte bt = 0; printf (toString (bt)); return 0; } // ==================================== The printf function should display the string "0", but it shows thesequence"0123456789".That's because toString() creates a D string, whose length is determined by the ".length" property. printf's first argument takes a 0 terminated string. To get what you want, printf("%.*s", toString(bt)); // %.*s will recognize a D string or: printf(toStringz(toString(bt))); // makes a 0 terminated string
Apr 26 2004