正しく機能するユーティリティC関数があります。
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);
そして、この呼び出しで文字列を渡すと、アクセス違反の例外が発生します。
UtilDisplayMessageEasy("TEST", "This is only a %s.", "TEST");
それでも、元の関数を次のように呼び出すと、正常に機能します。
UtilDisplayMessage("TEST", "This is only a %s.", 1, "TEST");
引数パラメーターとローカルパラメーターのどちらをva_start()に渡すかについて、本当にそのような根本的な違いはありますか?
また、変数パラメータはあまり安全ではなく、注意して使用する必要があることも承知していますが、この無害なものでアラームが鳴るようなことはありません。
この問題に関してご意見をお寄せいただきありがとうございます。