Assert macro Learning

Source: Internet
Author: User
Tags prototype definition
Assert () macro usageNote: assert is a macro, not a function. In the assert. h header file of C. The assert macro prototype is defined in <assert. h>. Its function is to terminate program execution if its condition returns an error. The prototype definition is as follows:
#include <assert.h>void assert( int expression );

The role of assert is to calculate the expression first. If its value is false (that is, 0), It prints an error message to the standard error stream stderr first, then, terminate the program by calling abort. Otherwise, assert () has no effect. Macro assert () is generally used to confirm the normal operation of the program, where the expression construction is true only when no error occurs. After debugging, you do not need to delete the assert () Statement from the source code, because the macro assert () definition is empty when ndebug is defined.[1] See the following program list badptr. C:

# Include <stdio. h> # include <assert. h> # include <stdlib. h> int main (void) {file * FP; FP = fopen ("test.txt", "W"); // open a file in writable mode, create a file with the same name as assert (FP) if it does not exist; // No error is found here: fclose (FP); FP = fopen ("noexitfile.txt", "R "); // open a file in read-only mode. If the file does not exist, assert (FP) fails to be opened. // The fclose (FP) error occurs here ); // The program will never be executed here to return 0;} [root @ localhost error_process] # GCC badptr. c [root @ localhost error_process] #. /. outa. out: badptr. c: 14: Main: assertion 'fp 'failed.

The disadvantage of using assert is that frequent calls will greatly affect program performance and increase additional overhead. After debugging, you can disable the assert call by inserting # define ndebug before the # include <assert. h> statement. The sample code is as follows:

#include <stdio.h>#define NDEBUG#include <assert.h>

Usage summary and precautions: 1) Check the validity of input parameters at the beginning of the function, for example:

Int resetbuffersize (INT nnewsize) {// function: Change the buffer size, // parameter: New length of the nnewsize buffer // return value: current length of the buffer // description: nnewsize <= 0 indicates clearing the buffer assert (nnewsize> = 0); Assert (nnewsize <= max_buffer_size );...}

2) Each assert only tests one condition, because when multiple conditions are verified simultaneously, if the assert fails, it is impossible to intuitively determine which condition fails:

assert(nOffset>=0 && nOffset+nSize<=m_nInfomationSize);
Good:
assert(nOffset >= 0);    assert(nOffset+nSize <= m_nInfomationSize);

3) You cannot use a statement to change the environment, because assert only takes effect in debug. If so, the application may encounter a problem during actual running: assert (I ++ <100) this is because if an error occurs, for example, I = 100 before execution, this statement will not be executed, and the I ++ command will not be executed. Correct: assert (I <100) I ++; 4) The Assert and the following statements should be empty for logical and visual consistency. 5) some places, assert cannot replace conditional FilteringNote:: For floating point:

#include<assert.h>// float pi=3.14;// assert(pi==3.14); //float pi=3.14f;assert (pi==3.14f);

---------------------------------------------------------

In a switch statement, there must always be a default clause to display information (assert ).
int number = SomeMethod();switch(number){case 1:Trace.WriteLine("Case 1:");break;case 2:Trace.WriteLine("Case 2:");break;default :Debug.Assert(false);break;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.