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.
- /*
- * Myverify. h
- * Self-implemented assertion macro-myverify
- * 2008.10.30
- * Kaoya
- */
- # Ifndef _ myverify_h __
- # DEFINE _ myverify_h __
- # Pragma comment (Lib, "USER32 ")
- # Include <crtdbg. h>
- # Ifdef _ debug
- # Define myverify (A) if (! (A) printerror (# A ,__ file __,__ line __, getlasterror ())
- # Else
- # Define myverify ()
- # Endif
- _ Inline void printerror (lpstr linedesc, lpstr filename, int lineno, DWORD errnum)
- {
- Lpstr lpbuffer;
- Char errbuf [256];
- Char modulename [max_path];
- DWORD numread;
- Formatmessage (format_message_allocate_buffer
- | Format_message_from_system,
- Null,
- Errnum,
- Lang_neutral,
- (Lptstr) & lpbuffer,
- 0,
- Null );
- Getmodulefilename (null, modulename, max_path );
- Wsprintf (errbuf, "/n % s occur failed at line % d in % s:/n"
- "% S/n/nreason: % s/n", modulename, lineno, filename, linedesc, lpbuffer );
- # Ifndef _ WINDOWS
- Writefile (getstdhandle (std_error_handle), errbuf, strlen (errbuf), & numread, false );
- Sleep (3000 );
- # Else
- MessageBox (getactivewindow (), errbuf, modulename, mb_iconwarning | mb_ OK | mb_taskmodal | mb_setforeground );
- # Endif
- Exit (exit_failure );
- }
- # Endif/_ myverify_h __