Self-implemented assert Program

Source: Internet
Author: User

Self-implemented asset (asset) Program

1. Overview

Many textbooks are called error-Proof Design for programmers during programming. In fact, this is also necessary. The error prevention design can effectively capture the invalid and error status when the program is running, so that the programmer can quickly locate the error message for further processing. It is hard to imagine how terrible a system of a certain scale is to have no mistake-proof design. There is a classic saying: Don't know, don't be terrible. The terrible thing is, don't know, don't know. This sentence is as classic as it is here: the program error is not terrible, but the terrible thing is that you do not know the error.

There are many error prevention designs, such as logging log files and dialog box error prompts. The error-proof design I want to talk about today is assert ).

2. Assert)

Programs are generally divided into debug and release versions. The debug version is used for internal debugging, And the release version is released to users. Assert is a macro that only works in the debug version. It is used to monitor what should not happen. In order not to cause any difference between the debug and release versions of the program, assert should not produce any side effects. So assert is not a function, but a macro. Programmers can regard assert as a harmless testing method that can be safely used in any system status.

2.1 various types of assertions

Both C and C ++ provide us with assertions:

◆ Ansi c asserted: assert function.

◆ C Runtime function library assertions: _ assert macro and _ asserte macro.

◆ Assertions of the MFC Library: assert macro, _ assert macro, verify macro, assert_valid macro, assert_kindof macro, assert_pointer macro, and assert_null_or_pointer macro.

◆ ATL assertions: atlassert

Today, I will detail how to use these assertions. You can query them online.

2.2 Use of assertions

Assertions are used to monitor errors, not to correct errors. We should use assertions with defense and detection. For example, the validity (validity) Check of function parameters and the correctness check of the return values of API function calls.

3, Self-implemented "assertions"

When I use assertions, I prefer to customize the assertions. For example, I can customize the error information to display on the standard output screen (similar to the command line window ), or displayed in the pop-up dialog box. You can customize the content of the error information, including the source code file name, line number, code, and prompt information. The following macro is an asserted macro customized by the author. If anything is wrong, forget your advice.

  1. /*
  2. * Myverify. h
  3. * Self-implemented assertion macro-myverify
  4. * 2008.10.30
  5. * Kaoya
  6. */
  7. # Ifndef _ myverify_h __
  8. # DEFINE _ myverify_h __
  9. # Pragma comment (Lib, "USER32 ")
  10. # Include <crtdbg. h>
  11. # Ifdef _ debug
  12. # Define myverify (A) if (! (A) printerror (# A ,__ file __,__ line __, getlasterror ())
  13. # Else
  14. # Define myverify ()
  15. # Endif
  16. _ Inline void printerror (lpstr linedesc, lpstr filename, int lineno, DWORD errnum)
  17. {
  18. Lpstr lpbuffer;
  19. Char errbuf [256];
  20. Char modulename [max_path];
  21. DWORD numread;
  22. Formatmessage (format_message_allocate_buffer
  23. | Format_message_from_system,
  24. Null,
  25. Errnum,
  26. Lang_neutral,
  27. (Lptstr) & lpbuffer,
  28. 0,
  29. Null );
  30. Getmodulefilename (null, modulename, max_path );
  31. Wsprintf (errbuf, "/n % s occur failed at line % d in % s:/n"
  32. "% S/n/nreason: % s/n", modulename, lineno, filename, linedesc, lpbuffer );
  33. # Ifndef _ WINDOWS
  34. Writefile (getstdhandle (std_error_handle), errbuf, strlen (errbuf), & numread, false );
  35. Sleep (3000 );
  36. # Else
  37. MessageBox (getactivewindow (), errbuf, modulename, mb_iconwarning | mb_ OK | mb_taskmodal | mb_setforeground );
  38. # Endif
  39. Exit (exit_failure );
  40. }
  41. # Endif/_ myverify_h __

 

 

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.