Describes the problems and skills of the C ++ Language

Source: Internet
Author: User

The current Chairman of the C ++ Language Standards Committee, Herb Sutter, and his predictional series have been published including: predictional C ++, More predictional C ++, and predictional C ++ Style ..

Active debugging and contract programming complement each other to jointly ensure the quality of software development. Contract programming is equivalent to various contracts signed in economic life, and active debugging is equivalent to the legal penalty measures taken when a party fails to comply with the contract.
Various development languages and development tools provide these debugging statements. The standard C ++ provides the assert function, and the MFC provides the ASSERT debugging macro to help us perform active debugging. In actual work, we recommend that you use the ASSERT debugging macro of MFC.

Parameter check
In addition to specifying a contract, you should check the input parameters at the beginning of the function to ensure that the error message is immediately reported when an invalid parameter is passed in. For example:

  1. BOOL GetPathItem ( int i , LPTSTR szItem , int iLen )  
  2. {  
  3. ASSERT ( i > 0 ) ;  
  4. ASSERT ( NULL != szItem ) ;  
  5. ASSERT ( ( iLen > 0 ) && ( iLen < MAX_PATH ) ) ;  
  6. ASSERT ( FALSE == IsBadWriteStringPtr ( szItem , iLen ) ) ;  

This check can only exclude null pointers. However, if the pointer points to an invalid address or the pointer points to an object that is not of the expected type, the above example cannot be checked out, but all think it is correct. The complete check should be as follows:

  1. // An example of checking only a part of the error condition  
  2. BOOL EnumerateListItems ( PFNELCALLBACK pfnCallback )  
  3. {  
  4. ASSERT ( NULL != pfnCallback ) ;  

The proper use of ASSERT in the code can greatly help bug detection and improve debugging efficiency. The following is a simple example.

  1. switch( nType )  
  2. {  
  3. case GK_ENTITY_POINT:  
  4. // do something  
  5. break;  
  6. case GK_ENTITY_PLINE:  
  7. // do something  
  8. break;  
  9. default:  
  10. ASSERT( 0 );  

In the preceding example, the switch statement only processes GK_ENTITY_POINT and GK_ENTITY_PLINE. The switch statement must only process these two conditions in the system at the time, however, if the system needs to deal with more situations in the future, and the above Code is not updated in time, or the developer has neglected it for a while.

A bug that may cause system errors or crashes occurs, and the use of ASSERT can promptly remind developers of their negligence and eliminate the bug as quickly as possible. In other cases, when developers write code, if you are sure that A is incorrect at A certain point, you can add ASSERT here to exclude situation.

  • Describes the most common menus in IE implemented by C ++ Builder
  • Visual C ++ 6.0 compilation implementation function
  • Analyze the relationship between C ++ syntax and C Language
  • Explanation of C ++ programming errors
  • Detailed introduction to the C ++ language and its Code for learning

In conclusion, the proper and flexible use of ASSERT for active debugging can greatly improve the stability and security of the program, reduce debugging time, and improve work efficiency. In conclusion, the proper and flexible use of ASSERT for active debugging can greatly improve the stability and security of the program, reduce debugging time, and improve work efficiency.

Related Article

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: 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.