Home  >  Article  >  Backend Development  >  C++ Assert() assertion mechanism principle and usage

C++ Assert() assertion mechanism principle and usage

黄舟
黄舟Original
2016-12-28 13:25:131739browse

MSDN原文如是说:

Evaluates an expression and, when the result is false, prints a diagnostic message and aborts the program.

(判断一个表达式,如果结果为假,输出诊断消息并中止程序。)

void assert(   
   int expression   
);

参数:Expression (including pointers) that evaluates to nonzero or 0.(表达式【包括指针】是非零或零)

原理:assert的作用是现计算表达式 expression ,如果其值为假(即为0),那么它先向stderr打印一条出错信息,然后通过调用 abort 来终止程序运行。

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 &#39;%s&#39;\n", test1 ); fflush( stdout );  
   analyze_string( test1 );  
   printf ( "Analyzing string &#39;%s&#39;\n", test2 ); fflush( stdout );  
   analyze_string( test2 );  
   printf ( "Analyzing string &#39;%s&#39;\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 != &#39;\0&#39; );       // Cannot be empty  
   assert( strlen( string ) > 2 );  // Length must exceed 2  
}

输出结果

Analyzing string &#39;abc&#39;  
Analyzing string &#39;(null)&#39;  
Assertion failed: string != NULL, file assert.cpp, line 25  
  
abnormal program termination

用法总结:

1)在函数开始处检验传入参数的合法性
如:

int resetBufferSize(int nNewSize)
{
  //功能:改变缓冲区大小,
  //参数:nNewSize 缓冲区新长度
//返回值:缓冲区当前长度
//说明:保持原信息内容不变     nNewSize03ba5ff8b0b2f011c7b32d8b8c5b3ffe= 0);
assert(nNewSize d726e2418c56769520e66d98ba4a0a1e=0 && nOffset+nSize2be039e2a5b334bcdbdb568ca784cac3= 0);
     assert(nOffset+nSize c5452563e1e1704bcce66bc405c219d8的语句之前插入 #define NDEBUG 来禁用assert调用,示例代码如下:
#include ade979de5fc0e1ca0540f360a64c230b
#define NDEBUG
#include b0f0cbacb9a934b30cb1dd71186a65c7

加入#define NDEBUG之后,上文第一个例子输出结果为:

Analyzing string &#39;abc&#39;  
Analyzing string &#39;(null)&#39;  
Analyzing string &#39;&#39;

在面试中经常用到的一个题目:

已知memcpy的函数为: void* memcpy(void *dest , const void* src , size_t count)其中dest是目的指针,src是源指针。不调用c++/c的memcpy库函数,请编写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;      
  
    //防止内存重复  
    assert(!(psrc<=pdst && pdst<psrc+count));      
    assert(!(pdst<=psrc && psrc<pdst+count));      
  
    while(count--)      
    {      
        *pdst = *psrc;      
        pdst++;      
        psrc++;      
    }      
    return dst;      
}

 以上就是C++ Assert()断言机制原理以及使用的内容,更多相关内容请关注PHP中文网(www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn