によって設定された長さIが正しく動作するユーティリティCの機能を持っている持っているときは動作しません:vsprintf_s()va_listのは、ローカル変数
void UtilDisplayMessage(char* strCaption, char* strMessageFormat, int iArgCount, ...)
{
// Initialize the variable arg list
va_list lstArgs;
va_start(lstArgs, iArgCount);
// Format the message
vsprintf_s(g_strMessage, UTIL_DEF_MESSAGE_SIZE, strMessageFormat, lstArgs);
// Destroy the variable arg list
va_end(lstArgs);
// Use formatted string here...
}
をしかし、私は「iArgCount」パラメータを削除したいと思いますので、私はしましたこのようになりますテスト関数を作った:
void UtilDisplayMessageEasy(char* strCaption, char* strMessageFormat, ...)
{
// Initialize the variable arg list
va_list lstArgs;
int iParamCount = 1;
va_start(lstArgs, iParamCount);
// Format the message
vsprintf_s(g_strMessage, UTIL_DEF_MESSAGE_SIZE, strMessageFormat, lstArgs);
// Destroy the variable arg list
va_end(lstArgs);
// Use formatted string here...
}
しかし、私はこの呼び出しで整数値を渡す場合、私は偽の結果を得る:
UtilDisplayMessageEasy("TEST", "The value is %i.", 1);
私はこの呼び出しで文字列を渡すとき
と私はアクセス違反の例外を取得:
UtilDisplayMessage("TEST", "This is only a %s.", 1, "TEST");
本当にあります:私はそうのように私の元の関数を呼び出す場合それでも
UtilDisplayMessageEasy("TEST", "This is only a %s.", "TEST");
、それが正常に動作しますva_start()に引数パラメータまたはローカルパラメータを渡すかどうかの基本的な違いはありますか?
また、可変パラメータはあまり安全ではなく、気をつけて使用する必要がありますが、この無害なものがアラームを鳴らすものではないはずです。
この問題に関してご意見をお寄せいただきありがとうございます。
'UtilDisplayMessageEasy'では' va_start(lstArgs、iParamCount);を 'va_start(lstArgs、strMessageFormat);に変更します。 –
ああ!それでおしまい!どうもありがとうございました! – BlazeCell