C++ Assert()斷言機(jī)制原理以及使用方法
MSDN原文如是說:
Evaluates an expression and, when the result is false, prints a diagnostic message and aborts the program.
(判斷一個表達(dá)式,如果結(jié)果為假,輸出診斷消息并中止程序。)
void assert( int expression );
參數(shù):Expression (including pointers) that evaluates to nonzero or 0.(表達(dá)式【包括指針】是非零或零)
原理:assert的作用是現(xiàn)計算表達(dá)式 expression ,如果其值為假(即為0),那么它先向stderr打印一條出錯信息,然后通過調(diào)用 abort 來終止程序運(yùn)行。
MSDN示例程序
// crt_assert.c
// compile with: /c
#include <stdio.h>
#include <assert.h>
#include <string.h>
void analyze_string( char *string ); // Prototype
int main( void )
{
char test1[] = "abc", *test2 = NULL, test3[] = "";
printf ( "Analyzing string '%s'\n", test1 ); fflush( stdout );
analyze_string( test1 );
printf ( "Analyzing string '%s'\n", test2 ); fflush( stdout );
analyze_string( test2 );
printf ( "Analyzing string '%s'\n", test3 ); fflush( stdout );
analyze_string( test3 );
}
// Tests a string to see if it is NULL,
// empty, or longer than 0 characters.
void analyze_string( char * string )
{
assert( string != NULL ); // Cannot be NULL
assert( *string != '\0' ); // Cannot be empty
assert( strlen( string ) > 2 ); // Length must exceed 2
}
輸出結(jié)果
Analyzing string 'abc' Analyzing string '(null)' Assertion failed: string != NULL, file assert.cpp, line 25 abnormal program termination
用法總結(jié):
1) 在函數(shù)開始處檢驗傳入?yún)?shù)的合法性
如:
int resetBufferSize(int nNewSize)
{
//功能:改變緩沖區(qū)大小,
//參數(shù):nNewSize 緩沖區(qū)新長度
//返回值:緩沖區(qū)當(dāng)前長度
//說明:保持原信息內(nèi)容不變 nNewSize<=0表示清除緩沖區(qū)
assert(nNewSize >= 0);
assert(nNewSize <= MAX_BUFFER_SIZE);
...
}
2) 每個assert只檢驗一個條件,因為同時檢驗多個條件時,如果斷言失敗,無法直觀的判斷是哪個條件失敗
不好: assert(nOffset>=0 && nOffset+nSize<=m_nInfomationSize);
好: assert(nOffset >= 0);
assert(nOffset+nSize <= m_nInfomationSize);
3) 不能使用改變環(huán)境的語句,因為assert只在DEBUG個生效,如果這么做,會使用程序在真正運(yùn)行時遇到問題
錯誤: assert(i++ < 100)
這是因為如果出錯,比如在執(zhí)行之前i=100,那么這條語句就不會執(zhí)行,那么i++這條命令就沒有執(zhí)行。
正確: assert(i < 100);
i++;
4) assert和后面的語句應(yīng)空一行,以形成邏輯和視覺上的一致感
5) 有的地方,assert不能代替條件過濾
ASSERT只有在Debug版本中才有效,如果編譯為Release版本則被忽略掉。(在C中,ASSERT是宏而不是函數(shù)),使用ASSERT“斷言”容易在debug時輸出程序錯誤所在。
而assert()的功能類似,它是ANSI C標(biāo)準(zhǔn)中規(guī)定的函數(shù),它與ASSERT的一個重要區(qū)別是可以用在Release版本中。
使用assert的缺點(diǎn)是,頻繁的調(diào)用會極大的影響程序的性能,增加額外的開銷。
在調(diào)試結(jié)束后,可以通過在包含#include <assert.h>的語句之前插入 #define NDEBUG 來禁用assert調(diào)用,示例代碼如下:
#include <stdio.h>
#define NDEBUG
#include <assert.h>
加入#define NDEBUG之后,上文第一個例子輸出結(jié)果為:
Analyzing string 'abc' Analyzing string '(null)' Analyzing string ''
在面試中經(jīng)常用到的一個題目:
已知memcpy的函數(shù)為: void* memcpy(void *dest , const void* src , size_t count)其中dest是目的指針,src是源指針。不調(diào)用c++/c的memcpy庫函數(shù),請編寫memcpy。
void* memcpy(void *dst, const void *src, size_t count)
{
//安全檢查
assert( (dst != NULL) && (src != NULL) );
unsigned char *pdst = (unsigned char *)dst;
const unsigned char *psrc = (const unsigned char *)src;
//防止內(nèi)存重復(fù)
assert(!(psrc<=pdst && pdst<psrc+count));
assert(!(pdst<=psrc && psrc<pdst+count));
while(count--)
{
*pdst = *psrc;
pdst++;
psrc++;
}
return dst;
}
以上這篇C++ Assert()斷言機(jī)制原理以及使用方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
2022最新使用VSCode編譯運(yùn)行C++的過程及會遇到的兩個問題
這篇文章主要介紹了2022最新使用VSCode編譯運(yùn)行C++的過程及會遇到的兩個問題,這里需要注意把剛才解壓的地址加上\bin添加進(jìn)去,比如我的:D:\aaakkk\cpp\mingw64\bin,然后點(diǎn)確定,注意一定要確保它被保存了,感興趣的朋友跟隨小編一起看看吧2022-09-09
解決c++?error:crosses?initialization?of?問題
最近在寫代碼的時候,碰到了?crosses?initialization?of?...?的問題,只因我在?switch?的某個?case?分支下定義了一個變量,于是乎便將這個問題整理一下,需要的朋友可以參考下2023-03-03

