April 26, 2004 Re: Bug in printf function? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Bruno A. Costa | "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 the sequence > "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 |
April 26, 2004 Re: Bug in printf function? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Stewart Gordon | "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. |
April 26, 2004 Re: Bug in printf function? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | "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... > > 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. Yes indeed. Someone needs to point that out in a book sometime, don't you think? |
April 26, 2004 Re: Bug in printf function? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Matthew | "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... > > > > "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. > > Yes indeed. Someone needs to point that out in a book sometime, don't you think? LOL! |
Copyright © 1999-2021 by the D Language Foundation