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);
assertexpression If the value is False (that is, 0stderr print an error message,
abort
Take a look at the following program listing 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 a writable manner and create a file with the same name if it does not exist
ASSERT (FP); //So there's no mistake here.
Fclose (FP);
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;FP = fopen ( "Noexitfile.txt", "R" )// Open a file in read-only mode, Open file fails if it does not exist
assert ( fp ); // So here's the error
fclose ( fp ); // program is never executed here.
return 0;
}
[Email protected] error_process]# gcc badptr.c
[Email protected] error_process]#./a.out
A.out:badptr.c:14:main:assertion ' FP ' failed.
has been abandoned
using assert The downside is that Frequent calls can greatly affect the performance of the program and add additional overhead.
at the end of debugging, you can do this by including insert # Define ndebug assert call, 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 :
int resetbuffersize (int nnewsize)
{
//Function:Changing the buffer size,
// parameters :nnewsize buffer new length
// return value : buffer current length
// description : Keep the original information content unchanged nnewsize<=0 means clear buffer
assert (nnewsize >= 0);
assert (nnewsize <= max_buffer_size);
...
}
2) each assert examines only one condition , because when multiple conditions are checked , if the assertion fails , it is not possible to visually determine which condition failed
Bad : Assert (noffset>=0 && noffset+nsize<=m_ninfomationsize);
Good : Assert (noffset >= 0);
ASSERT (Noffset+nsize <= m_ninfomationsize);
3)Cannot use statements that alter the environment,BecauseAssertOnly inDEBUGA valid,If you do this,,Use the program to run into problems when it is actually running
Error: Assert (i++ < 100)
This is because if something goes wrong, like before the executioni=100, Then this statement will not be executed, then i++ This command is not executed.
correct : ASSERT (i < 100)
i++;
4) assert and the following statements should be empty one line , To form a logical and visual sense of consistency
5) in some places , assert cannot replace conditional filtering
Article Source:http://www.diybl.com/course/3_program/c++/cppjs/20071111/85534.html
Excerpt from the high-quality C + + Programming GuidePage 41-42 ...
Procedures are generally divided intoDebugVersions andReleaseVersiondebug version for internal debugging, release version released to users.
assert assert is only in debug Version of the macro that is working, it is used to check " should not " 6-5 is a memory copy function. During the run, if assert The parameters are false, Then the program will abort (there will also be a prompt dialogue, where the assert
void *memcpy (void *pvto, const void *pvfrom, size_t size)
{
ASSERT ((pvto! = null) && (Pvfrom! = null)); //Using assertions
byte *pbto = (BYTE *) pvto; //Prevent changePvtoThe address
byte *pbfrom = (byte *) pvfrom; // pvfrom address
while (size -- > 0)
*pbto ++ = *pbfrom ++ ;
return pvto;
}
example 6-5 Copy non-overlapping blocks of memory
assert debug version and release version caused the difference, assert should not have any side effects. So assert assertassert was terminated, not to say that contains the assert assert
There are few things that are more frustrating than tracking down a program's assertions, but not knowing what the assertion does. You have spent a lot of time not to rule out mistakes, but just to figure out what this error is. Sometimes, programmers sometimes design false assertions. So if you can't figure out what the assertion checks, it's hard to tell if the error is in the program or in the assertion. Fortunately, this is a good problem to solve, just add a clear note. This is the obvious thing, but few programmers do it. This is like a man in the forest, see the tree is nailed a " dangerous " big sign. But what exactly is the danger? The tree to pour? Have a waste well? A beast? Unless you tell people what "dangerous " is, the warning sign is difficult to play a positive and effective role. Hard-to-understand assertions are often ignored by programmers and even deleted.
The rules6-5-1"Use assertions to catch illegal situations that should not occur." Do not confuse the difference between the illegal situation and the wrong situation, the latter being inevitable and must be dealt with.
"rule 6-5-2 "at the entrance of the function, use assertions to check the validity of the parameters (legitimacy).
"recommended 6-5-1" What assumptions do I intend to make? " Once the assumptions are established, an assertion is used to check the assumptions.
"recommended 6-5-2 "general textbooks encourage programmers to make error-proof designs, but keep in mind that this style of programming may conceal errors. When making the error-proof design, if the " cannot happen "
the elements that should be included in the ASSERT macro:
Determine the condition, the location where the current assertion failed (file, number of rows, etc.), return an error, terminate the program ...
Several typical ASSERT notation:
VC in the wording:
#define assert (f) \
do \
{ \
if (! ( f) && afxassertfailedline (this_file, __line__)) \
afxdebugbreak () \
} while (0) \
#define _ASSERT (expr) \
do {if (!) ( Expr) && \
(1 = = _CrtDbgReport (_crt_assert, __file__, __line__, NULL, NULL))) \
_CrtDbgBreak (); } while (0)
How other platforms are written:
# define ASSERT (x) ((x) | | (dbg_printf ("Assertion failed (" __file__ ":%d): \"%s\ "\ n", __line__, #x), Break_point (), FALSE))
Original website address :http://hi.baidu.com/uwbadnhctpkopwr/item/1d5d1e1d316c79fcdceeca0a
C + + assert () How to use