Jump to page: 1 2
Thread overview
Bug in printf function?
Apr 26, 2004
Bruno A. Costa
Apr 26, 2004
J Anderson
Apr 26, 2004
Ant
Apr 26, 2004
J Anderson
Apr 26, 2004
Stewart Gordon
Apr 26, 2004
J Anderson
Apr 26, 2004
Vathix
Apr 26, 2004
Bruno A. Costa
Apr 26, 2004
Vathix
Apr 26, 2004
Stewart Gordon
Apr 26, 2004
Walter
Apr 26, 2004
Matthew
Apr 26, 2004
Walter
Apr 26, 2004
Walter
April 26, 2004
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.
April 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/
April 26, 2004
In article <c6ivk0$2roe$1@digitaldaemon.com>, J Anderson says...
>
>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);

(I don't have D here)

will it print correctly with
printf (toString (bt)~'\0');   // or "\0"
?

Ant


April 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.
April 26, 2004
Ant wrote:

>
>
>(I don't have D here)
>
>will it print correctly with printf (toString (bt)~'\0');   // or "\0"
>?
>
>Ant
>

You 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/
April 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/
April 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.
April 26, 2004
On Mon, 26 Apr 2004 21:03:02 +0800, J Anderson <REMOVEanderson@badmama.com.au> wrote:

> 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.
>
>

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. Miller
April 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
April 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.
« First   ‹ Prev
1 2