Re: _stprintf
- From: Joseph M. Newcomer <newcomer@xxxxxxxxxxxx>
- Date: Sat, 29 Jul 2006 22:51:43 -0400
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,Joseph M. Newcomer [MVP]
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
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
.
- Follow-Ups:
- Re: _stprintf
- From: Norman Diamond
- Re: _stprintf
- From: Mihai N.
- Re: _stprintf
- From: Vipin
- Re: _stprintf
- Prev by Date: Re: Open, process & print documents without user interface
- Next by Date: Re: CClientDC and TabCtrl ?
- Previous by thread: Re: _stprintf
- Next by thread: Re: _stprintf
- Index(es):
Relevant Pages
|
Loading