不戒庙

求助:CString Format自身的溢出的问题

以下是代码

1 CString strMsg1 = "确认要删除 %s 吗?";
2 CString title = "标题";
3 StrMsg1.Format(strMsg1,title");
以上这段代码出错
Expression:("Buffer too small",0)

而换成

1 CString strMsg1 ;
2 CString title = "标题";
3 StrMsg1.Format("确认要删除 %s 吗?",title");
就不出错了

环境:Win2k3_En_Sp2 + VS2005

各位达人,能告诉我是什么原因。

posted on 2008-07-19 11:07 不戒大师 阅读(3602) 评论(9)  编辑 收藏 引用

Feedback

# re: 求助:CString Format自身的溢出的问题 2008-07-19 11:35 斯卡

不明白你的title后为什么要加"  回复  更多评论   

# re: 求助:CString Format自身的溢出的问题 2008-07-19 12:06 三不戒大师

title值是变量啊,并不是固定值
  回复  更多评论   

# re: 求助:CString Format自身的溢出的问题[未登录] 2008-07-19 12:15 Kevin

title 不是字符串对象, 而不是char指针, 你应该 (LPCSTR)title 才对, 因为CString重载了LPCSTR, 这样在运行时才能自动转换成char指针, 匹配到 %s  回复  更多评论   

# re: 求助:CString Format自身的溢出的问题[未登录] 2008-07-19 12:16 Kevin

改一下"title 不是字符串对象" 应为 "title是CString对象"  回复  更多评论   

# re: 求助:CString Format自身的溢出的问题 2008-07-19 12:40 陈梓瀚(vczh)

CString能隐式转换到LPCTSTR的,这个倒不是问题。难道CString非空的时候,Format就默认『该对象被重新利用,而不是重新构造一个新的CString对象』?  回复  更多评论   

# re: 求助:CString Format自身的溢出的问题 2008-07-19 12:54 wxyz

CString::Format
void Format( LPCTSTR lpszFormat, ... );

void Format( UINT nFormatID, ... );

Parameters

lpszFormat

A format-control string.

nFormatID

The string resource identifier that contains the format-control string.

Remarks

Call this member function to write formatted data to a CString in the same way that sprintf formats data into a C-style character array. This function formats and stores a series of characters and values in the CString. Each optional argument (if any) is converted and output according to the corresponding format specification in lpszFormat or from the string resource identified by nFormatID.
////!!!!!这地方好像说了.
The call will fail if the string object itself is offered as a parameter to Format. For example, the following code:

CString str = "Some Data";
str.Format("%s%d", str, 123); // Attention: str is also used in the parameter list.

will cause unpredictable results.
////!!!!
When you pass a character string as an optional argument, you must cast it explicitly as LPCTSTR. The format has the same form and function as the format argument for the printf function. (For a description of the format and arguments, seeprintf in the Run-Time Library Reference.) A null character is appended to the end of the characters written.

  回复  更多评论   

# re: 求助:CString Format自身的溢出的问题 2008-07-19 16:01 三不戒大师

@wxyz
谢谢,终于弄明白是怎么回事了  回复  更多评论   

# re: 求助:CString Format自身的溢出的问题 2008-07-19 17:01 陈梓瀚(vczh)

经常看到msdn里面把bug当成feature……  回复  更多评论   

# re: 求助:CString Format自身的溢出的问题[未登录] 2008-07-20 08:55 Leo

@陈梓瀚(vczh)
这本来就是一种未定义情况,如同snprintf中将自己作为一个参数一样…  回复  更多评论   



只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


My Links

Blog Stats

常用链接

留言簿

随笔档案

搜索

最新评论

阅读排行榜

评论排行榜