_vsnprintf崩溃

flamkuavos 2012-11-14 02:09:15
#include <string>
using namespace std;
#include <stdarg.h>

inline std::string mystr_fmt(const char* fmt, ...)
{
va_list argptr;
va_start(argptr, fmt);
int rst_size = _vsnprintf(NULL, 0, fmt, argptr) + 2; //运行到这里要崩溃,咋回事?
char* rst_tmp = new char[rst_size];
_vsnprintf(rst_tmp, rst_size, fmt, argptr);
string rst(rst_tmp);
delete[] rst_tmp;
rst_tmp = NULL;
va_end(argptr);
return rst;
}

void main()
{
mystr_fmt("%d", 2);
}


此程序可以运行,VC6
...全文
987 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
swordtan 2012-11-15
  • 打赏
  • 举报
回复
引用 1 楼 gohay1 的回复:
vsnprintf,_vsnprintf, and _vsnwprintf return the number of characters written if the number of characters to write is less than or equal to count; if the number of characters to write is ……
++ http://msdn.microsoft.com/en-us/library/1kt27hek.aspx http://msdn.microsoft.com/en-us/library/ksazx244.aspx
sduxiaoxiang 2012-11-15
  • 打赏
  • 举报
回复
远离VC6吧
JiMoKuangXiangQu 2012-11-14
  • 打赏
  • 举报
回复
#include <string> 
using namespace std; 
#include <stdarg.h>   

inline std::string mystr_fmt(const char* fmt, ...) 
{     
	va_list argptr;     
	va_start(argptr, fmt);     
	int rst_size = _vsnprintf(NULL, 0, fmt, argptr) + 2; //运行到这里要崩溃,咋回事?     
	char* rst_tmp = new char[rst_size];     
	_vsnprintf(rst_tmp, rst_size, fmt, argptr);     
	string rst(rst_tmp);     
	delete[] rst_tmp;     
	rst_tmp = NULL;     
	va_end(argptr);     
	return rst; 
}   

void main() 
{     
	mystr_fmt("%d", 2); 
	return;
}
用vs2008跑了下,程序可以运行.
gohay1 2012-11-14
  • 打赏
  • 举报
回复
vsnprintf,_vsnprintf, and _vsnwprintf return the number of characters written if the number of characters to write is less than or equal to count; if the number of characters to write is greater than count, these functions return -1 indicating that output has been truncated. The return value does not include the terminating null, if one is written. If buffer or format is NULL, or if count is less than or equal to zero, these functions invoke the invalid parameter handler, as described in Parameter Validation. If execution is allowed to continue, these functions return -1 and set errno to EINVAL.

64,691

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧