_ASSERT、、 _ASSERTE_ASSERT_EXPR

評估運算式,並在結果為 false 時產生偵錯報告 (僅限偵錯版本)。

語法

// Typical usage:
_ASSERT_EXPR( booleanExpression, message );
_ASSERT( booleanExpression );
_ASSERTE( booleanExpression );

參數

booleanExpression
評估為非零 (true) 或 0 (false) 的純量運算式 (包括指標運算式)。

message
顯示在報表中的寬字串。

備註

_ASSERT_EXPR, 、 _ASSERT_ASSERTE 巨集可以讓您在偵錯程序期間,利用清楚簡單的機制來檢查假設。 它們具有彈性,因為它們不需要包含在 語句中 #ifdef ,以防止在應用程式的零售組建中呼叫它們。 使用 宏可 _DEBUG 達成此彈性。 僅當_ASSERT_EXPR, _ASSERT_ASSERTE_DEBUG 。 未定義時 _DEBUG ,會在前置處理期間移除對這些宏的呼叫。

_ASSERT_EXPR_ASSERT_ASSERTE 評估其 booleanExpression 引數,並在結果為 false (0) 時,列印診斷訊息並呼叫 _CrtDbgReportW 以產生偵錯報告。 宏 _ASSERT 會列印簡單的診斷訊息、 _ASSERTE 在訊息中包含失敗運算式的字串表示,以及 _ASSERT_EXPR 包含 message 診斷訊息中的字串。 當 booleanExpression 評估為非零值時,這些巨集不會執行任何動作。

_ASSERT_EXPR, _ASSERT_ASSERTE 會叫用 _CrtDbgReportW,讓所有的輸出都呈現寬字元。 _ASSERTE 會正確地列印 booleanExpression 中的 Unicode 字元,而 _ASSERT_EXPR 則會列印 message中的 Unicode 字元。

因為 _ASSERTE 巨集會指定失敗的運算式,而 _ASSERT_EXPR 則可讓您指定所產生之報表中的訊息,所以使用者無須參考應用程式的原始程式碼,就能找出問題。 但其缺點在於,每一個由 message 所列印的 _ASSERT_EXPR 及每一個由 _ASSERTE 評估的運算式都會以字串常數形式包含在您應用程式的輸出 (偵錯版本) 檔案中。 因此,當對 _ASSERT_EXPR_ASSERTE的呼叫十分大量時,這些運算式可能會讓您的輸出檔案大小大幅增大。

除非您以 和 _CrtSetReportFile 函式指定 , _CrtSetReportMode 否則訊息會出現在相當於設定的快顯對話方塊中:

_CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_WNDW);

_CrtDbgReportW 依據目前的報表模式及為 _CRT_ASSERT 報告類型所定義的檔案產生偵錯報表,並判斷其目的地。 判斷提示失敗及錯誤預設會導向到偵錯訊息視窗。 和 _CrtSetReportFile_CrtSetReportMode 式可用來定義每個報表類型的目的地。

當目的地是偵錯訊息視窗,而使用者選取 [重試 ] 按鈕時, _CrtDbgReportW 會傳回 1,當 _ASSERT_EXPR_ASSERT 啟用 Just-In-Time 偵錯時,會導致 和 _ASSERTE 宏啟動偵錯工具。

如需報告程式的詳細資訊,請參閱 _CrtDbgReport_CrtDbgReportW 函式。 如需解決判斷提示失敗以及使用這些宏作為偵錯錯誤處理機制的詳細資訊,請參閱 報告宏

除了 _ASSERT 宏之外, assert 宏也可以用來驗證程式邏輯。 此巨集可在偵錯版與發行版的程式庫中使用。 、 _RPT_RPTF 偵錯宏也可用於產生偵錯報表,但不會評估運算式。 _RPT 巨集會產生簡單的報表。 _RPTF 巨集會在所產生的報表中,加入來源檔案及報表巨集呼叫所在的行號。 此外也提供這些巨集的寬字元版本 (_RPTW_RPTFW)。 寬字元版本與窄字元版本相同,但所有字串參數及輸出皆會使用寬字元字串。

雖然 _ASSERT_EXPR_ASSERT_ASSERTE 是宏,而且可以包含 <crtdbg.h> ,但是當定義 時 _DEBUG ,應用程式必須與 C 執行時間程式庫的偵錯版本連結,因為這些宏會呼叫其他執行時間函式。

需求

Macro 必要的標頭
_ASSERT_EXPR, _ASSERT, _ASSERTE <crtdbg.h>

範例

此程式會呼叫 _ASSERT_ASSERTE 巨集來測試條件 string1 == string2。 若該條件失敗時,這些巨集便會列印診斷訊息。 此程式中也會執行 _RPT_RPTF 巨集群組,作為 printf 函式的替代函式。

// crt_ASSERT_macro.c
// compile with: /D_DEBUG /MTd /Od /Zi /link /verbose:lib /debug
//
// This program uses the _ASSERT and _ASSERTE debugging macros.
//

#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <crtdbg.h>

int main()
{
   char *p1, *p2;

   // The Reporting Mode and File must be specified
   // before generating a debug report via an assert
   // or report macro.
   // This program sends all report types to STDOUT.
   _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE);
   _CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDOUT);
   _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_FILE);
   _CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDOUT);
   _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE);
   _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDOUT);

   // Allocate and assign the pointer variables.
   p1 = (char *)malloc(10);
   strcpy_s(p1, 10, "I am p1");
   p2 = (char *)malloc(10);
   strcpy_s(p2, 10, "I am p2");

   // Use the report macros as a debugging
   // warning mechanism, similar to printf.
   // Use the assert macros to check if the
   // p1 and p2 variables are equivalent.
   // If the expression fails, _ASSERTE will
   // include a string representation of the
   // failed expression in the report.
   // _ASSERT does not include the
   // expression in the generated report.
   _RPT0(_CRT_WARN,
       "Use the assert macros to evaluate the expression p1 == p2.\n");
   _RPTF2(_CRT_WARN, "\n Will _ASSERT find '%s' == '%s' ?\n", p1, p2);
   _ASSERT(p1 == p2);

   _RPTF2(_CRT_WARN, "\n\n Will _ASSERTE find '%s' == '%s' ?\n",
          p1, p2);
   _ASSERTE(p1 == p2);

   _RPT2(_CRT_ERROR, "'%s' != '%s'\n", p1, p2);

   free(p2);
   free(p1);

   return 0;
}
Use the assert macros to evaluate the expression p1 == p2.
crt_ASSERT_macro.c(54) :
Will _ASSERT find 'I am p1' == 'I am p2' ?
crt_ASSERT_macro.c(55) : Assertion failed!
crt_ASSERT_macro.c(58) :

Will _ASSERTE find 'I am p1' == 'I am p2' ?
crt_ASSERT_macro.c(59) : Assertion failed: p1 == p2
'I am p1' != 'I am p2'

另請參閱

偵錯常式
assert宏、 、 _assert_wassert
_RPT、、 _RPTF_RPTW_RPTFW