posts - 25,  comments - 21,  trackbacks - 0

#include "stdafx.h"

#include <stdarg.h>

#define STR_MAX 1024
void FormatOutput(TCHAR * formatstring, ...)
{
int nSize = 0;
TCHAR *buff=new TCHAR[STR_MAX];
//
va_list args;
va_start(args, formatstring);
nSize = _vsntprintf_s( buff, STR_MAX*sizeof(TCHAR), STR_MAX-1, formatstring, args);
_tprintf(L"nSize: %d, buff: %s\n", nSize, buff);
va_end(args);
//
delete [] buff;
}
int _tmain(int argc, _TCHAR* argv[])
{
int x=1,y=2;
FormatOutput(L"%d+%d=%d",x,y,x+y);
return 0;
}

一个简单的调用,导致_vsntprintf_s出现内存越界,百思不得其解,在unicode时就是调用int _vsnwprintf_s( wchar_t *buffer, size_t sizeOfBuffer, size_t count, const wchar_t *format, va_list argptr ),这是在msdn上对_vsnwprintf_s函数的声明,第二个参数是sizeOfBuffer;





但你进行代码跟踪,跟进vswprint.c时,发现

int __cdecl _vsnwprintf_s (
        wchar_t *string,
        size_t sizeInWords,
        size_t count,
        const wchar_t *format,
        va_list ap
        )
{
    return _vsnwprintf_s_l(string, sizeInWords, count, format, NULL, ap);
}

第二个参数变成了"sizeInWords";

事情明了了,第二个参数应该是多少个TCHAR,不是buffer的size。所以最上面的代码调用_vsntprintf_s时第二个参数用STR_MAX就可以了。

posted on 2008-05-03 09:49 Sherk 阅读(2621) 评论(1)  编辑 收藏 引用

FeedBack:
# re: VS2005下Unicode形式时_vsntprintf_s调用出现内存越界
2008-08-29 23:52 | 疯子阿虹
永远记住,在对字符串操作的时候不要使用sizeof,而是_countof!!!
永远记住,在对字符串操作的时候不要使用sizeof,而是_countof!!!
永远记住,在对字符串操作的时候不要使用sizeof,而是_countof!!!
永远记住,在对字符串操作的时候不要使用sizeof,而是_countof!!!
永远记住,在对字符串操作的时候不要使用sizeof,而是_countof!!!
永远记住,在对字符串操作的时候不要使用sizeof,而是_countof!!!
永远记住,在对字符串操作的时候不要使用sizeof,而是_countof!!!
永远记住,在对字符串操作的时候不要使用sizeof,而是_countof!!!
  回复  更多评论
  
只有注册用户登录后才能发表评论。
<2024年4月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

常用链接

留言簿(3)

随笔档案

相册

好友

搜索

  •  

最新评论

阅读排行榜

评论排行榜