On the use of the Assert () function

Source: Internet
Author: User

The Assert function is used today to implement the strcpy function

1#include <stdio.h>2#include <assert.h>3 4 Char* STRCPY (Char*DST,Const Char*SRC)5 {6ASSERT (DST! = NULL && src! = null);7     Char*ret = DST;8      while((*dst++=*src++)! =' /');9     returnret;Ten}

Where assert is an assertion, is to see if the latter is set up, if it continues to run, otherwise it will not execute, that is, the assertion failed

Again to search, found a more detailed assert () function Usage Summary, Bo Lord for Glory, deposited in this, with everyone to encourage;

Link to http://www.cnblogs.com/ggzss/archive/2011/08/18/2145017.html#3269450

The prototype of an Assert macro is defined in <assert.h> , and its function is to terminate the execution of the program if its condition returns an error, and the prototype defines:

#include <assert.h>

void assert (int expression);

  assert the function is to calculate the expression now expression if the value is False (that is, the 0 ), then it first stderr prints an error message and then calls the Abort to terminate the program run. Take a look at the following program listing badptr.c:

Copy Code

#include <stdio.h>#include<assert.h>#include<stdlib.h>intMainvoid) {FILE*FP; FP= fopen ("Test.txt","W");//open a file in a writable manner and create a file with the same name if it does not existassert (FP); //so there's no mistake here .fclose (FP); FP= fopen ("Noexitfile.txt","R");//Open a file in a read-only manner and open it if it does not existassert (FP); //so there 's a mistake.fclose (FP); //The program will never be executed here.       return 0;}

Operation Result:

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

#include <stdio.h>

#define Ndebug

#include <assert.h>

Usage Summary and Precautions:

  1 verify the legitimacy of incoming parameters at the beginning of the function, such as:

Copy Code

 int   Nnewsize) { //  function: Change buffer size,  //  parameter: Nnewsize buffer new length  //  return value: Buffer current length  //  Description: Keep the contents of the original information intact nnewsize<=0 means clear buffer   >= 0  );  ASSERT (nnewsize  <= max_buffer_size); ...}

  2 ) Each assert only one condition is checked, because when multiple conditions are examined, if the assertion fails, it is not possible to visually determine which condition failed, such as:

Not good:

ASSERT (noffset>=0 && noffset+nsize<=m_ninfomationsize);

Good:

ASSERT (noffset >= 0);

ASSERT (Noffset+nsize <= m_ninfomationsize);

  3 You cannot use a statement that alters the environment because assert only in DEBUG If you do this, you will be using a program that is experiencing problems when it is actually running, such as:

Error:

ASSERT (i++ < 100);

This is because if there is an error, such as i=100before execution,then this statement will not be executed, then i++ This command will not be executed.

That's right:

ASSERT (I < 100);

i++;

  4 ) assert and subsequent statements should be blank lines to create a logical and visual sense of consistency.

  5 ) in some places, assert cannot replace conditional filtering. assert is used to avoid obvious errors, not to handle exceptions. Errors and exceptions are not the same, errors should not occur, exceptions are unavoidable. C language anomalies can be handled by conditional judgment, and other languages have their own exception handling mechanisms.

A very simple rule of using assert is that at the very beginning of a method or function, if you use it in the middle of the method, you need to consider carefully whether it should be. At the beginning of the method, a functional process was not started, and the problems that occurred during the execution of a functional process were almost abnormal.

On the use of the Assert () function

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.