求助:CString Format自身的溢出的問題
2 CString title = "標題";
3 StrMsg1.Format(strMsg1,title");
Expression:("Buffer too small",0)
而換成
2 CString title = "標題";
3 StrMsg1.Format("確認要刪除 %s 嗎?",title");
環境:Win2k3_En_Sp2 + VS2005
各位達人,能告訴我是什么原因。
title 不是字符串對象, 而不是char指針, 你應該 (LPCSTR)title 才對, 因為CString重載了LPCSTR, 這樣在運行時才能自動轉換成char指針, 匹配到 %s 回復 更多評論
改一下"title 不是字符串對象" 應為 "title是CString對象" 回復 更多評論
CString能隱式轉換到LPCTSTR的,這個倒不是問題。難道CString非空的時候,Format就默認『該對象被重新利用,而不是重新構造一個新的CString對象』? 回復 更多評論
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.
回復 更多評論
經常看到msdn里面把bug當成feature…… 回復 更多評論
@陳梓瀚(vczh)
這本來就是一種未定義情況,如同snprintf中將自己作為一個參數一樣… 回復 更多評論
只有注冊用戶登錄后才能發表評論。 | ||
【推薦】100%開源!大型工業跨平臺軟件C++源碼提供,建模,組態!
![]() |
||
網站導航:
博客園
IT新聞
BlogJava
博問
Chat2DB
管理
|
||
|