Re: _stprintf

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



It cannot be causing a "storage leak" since there is no allocation involved.

You should generally avoid _stprintf whenever possible. Use CString::Format as the
preferred choice. If you MUST use some form like _stprintf, use StringCchPrintf (I think
that's the name, but search for strsafe.h on the MSDN) which at least will avoid any
possibility of buffer overflow

It would help a lot if you did something like

StringCchPrintf(_T("%c"), B, sizeof(B) / sizeof(TCHAR), (BYTE)('a' + i));

This eliminates several problems:
1. No buffer overflow if you make an error
2. The value is not treated as a signed value and therefore sign extended
(see problem 1)

However, your actual problem is even simpler; it would have been better to do
TCHAR B[2];
B[0] = _T('a') + i;
B[1] = _T('\'0');

which eliminates the need to call a subroutine to do a fundamentally trivial computation.

I assume you meant %c because %a is not documented.
joe

On Sat, 29 Jul 2006 13:17:02 -0700, Sonu <sonu@xxxxxxxxxxxxx> wrote:

Hello all,
I am having trouble with _stprintf(). It's a Unicode application.

TCHAR B[5];
_stprintf( B, _T("%a"), 'a' + i);

so if the value if i=9, I want B to be "j". But instead the value in B is
"-0x1.c39360p+117"

And this is causing all kinds of memory leaks and strange behaviours...

Any help appreciated
Thanks
Srishti
Joseph M. Newcomer [MVP]
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
.



Relevant Pages

  • Re: Do you Sell on eBay? Get this fabulous Pixel-ad - lasts for ever
    ... Flaming really isn't worth ... I never let them bother me, and by responding to them, I would give ... the problems that people are causing me or thinking they are ... will say offensive things to something, I try to avoid that subject. ...
    (alt.marketing.online.ebay)
  • Re: Requesting Certificate Error
    ... which could be causing this. ... Maybe someone else can jump in with further ... I think the problem is IE on the Server - it's just too locked down. ... that the CEICW will do the job, and avoid the MSExchange.org article ...
    (microsoft.public.windows.server.sbs)
  • Re: Messages Stuck in The Queue
    ... restarting the server. ... What's causing this to happen? ... How to avoid this from ...
    (microsoft.public.exchange.admin)
  • Re: Efficient String Lookup?
    ... Chris S. wrote: ... > expert with rex, ... > causing this? ... To avoid this, one would have to add to the patterns \b AFAIR, so that it ...
    (comp.lang.python)