[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[avr-gcc-list] Re: sprintf

From: David Brown
Subject: [avr-gcc-list] Re: sprintf
Date: Fri, 27 Feb 2009 22:10:16 +0100
User-agent: Thunderbird (Windows/20081209)

David VanHorn wrote:

     > I solved my problem by inserting the null with
     >  memset((A_String+16), 0, 1); and the same for B_String.

    A_String[16] = 0;

    would be a much more efficient way of performing the same operation.

Interesting.. I would expect this to result in a loop writing out a bunch of zeroes. I'm going to play with this some more tomorrow when I'm not tired, and see if I can make sense of how the strings are getting munged. It makes sense to plant the trailing zero on every write by sprintf, and that does seem to fit with the symptoms. I was thinking that it was ONLY writing the char I gave it.

There is nothing in the code that you posted so far to indicate that sprintf or memset are remotely sane ways to write this code. The code:

        sprintf((A_String + i), "%c", 0xff)

has exactly the same effect as:

        A_String[i] = 0xff;
        A_String[i + 1] = 0x00;

        memset((A_String + 16), 0, 1);

has the same effect as :

        A_String[16] = 0x00;

My estimates might be a bit off, but you get the idea.

And again, if you want help, post the actual code, including relevant function and variable declarations, and required standard #includes. For example, if you had given us the definition of A_String, we could tell you if there is a buffer overflow in your code. If I can take your code, cut and paste it into a C file, and compile it myself, then I can give much more useful advice.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]