Whenever the program gets aborted because of an assertion, a corefile would be dumped. The developer can analyze the corefile (using any debugger, for example 'GDB') and pin-point the exact location of the failure, and then figure out why something unexpected happened. Thus assertions can be very useful for the developer during the early stages of development of a program.
Note that assertions would be enabled only during development and early stages of testing; they would be normally disabled during final testing. Assertions would always be disbaled when the final product is delivered to the customer.
How assertions are different from error handling
Assertions should be used to document logically impossible situations and discover programming errors— if the "impossible" occurs, then something fundamental is clearly wrong. This is distinct from error handling: most error conditions are possible, although some may be extremely unlikely to occur in practice. Using assertions as a general-purpose error handling mechanism is unwise: assertions do not allow for recovery from errors; an assertion failure will normally halt the program's execution abruptly. Assertions also do not display a user-friendly error message.
What is assert()?
Assert() is a macro defined in assert.h, as follows
void assert (int)
If the value (normally the result of the expression) passed is non-zero, nothing happens. However if the value happens to be zero, it prints an error message similar to the following one to STDERR and aborts the program
Assertion Failed: file
The simplest and most effective use of assertion is to specify and check input conditions to functions such as to check
- Pointers are not null
- Indexes and size values are non-negative and less than a known limit
- By default,ANCI C compilers generate code to check assertions at run time.
- Assertion checking can be turned off by defining the NDEBUG flag to the compiler.
- This can be done either by inserting #define NDEBUG in a heder file such as stdhdr.h or while compiling use the option -dNDEBUG as
Enter the 2 numbers to divide
Sample1: assert_test.c:8: main: Assertion `y!=0' failed.
In this program if the user provides the value 0 for y, then assert() would get triggered and will abort the program.
The following code
can be better written along with a string indicating what is the reason for the assertion, as
Question: Whats wrong with this code
Answer: When the code is compiled with assertions deisabled, index variable wouldn't get incremented and would result in an infinite loop. So never write any real code within an assert statement