0% found this document useful (0 votes)
33 views

Discussion On Asserts

This document discusses asserts and how they are used in C programming. Asserts are macros used to add diagnostics to a program by checking for conditions that should be true. An assertion failure will abort the program and print an error message including the failed expression, file, and line number. Asserts are useful for testing pre-conditions and post-conditions during development but are typically compiled out of production code for performance reasons. The document covers best practices for using asserts and how to customize assert error messages.

Uploaded by

Aditya Barve
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

Discussion On Asserts

This document discusses asserts and how they are used in C programming. Asserts are macros used to add diagnostics to a program by checking for conditions that should be true. An assertion failure will abort the program and print an error message including the failed expression, file, and line number. Asserts are useful for testing pre-conditions and post-conditions during development but are typically compiled out of production code for performance reasons. The document covers best practices for using asserts and how to customize assert error messages.

Uploaded by

Aditya Barve
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Discussion on Asserts

Agenda

Background
What is Assert
Usage of Assert/Code snippets
Customizing Assert
How not to use Asserts
Evils of asserts
References

Background
Defensive programming:
Anticipating failures
Add supporting code to detect/ isolate
failures

Error Handling
Adding diagnostics to the program

What is Assert
State a fact confidently and forcefully
In C- Assert is a macro used to add
diagnostics to the program
An assertion is a Boolean expression
at a specific point in a program that
will be true unless there is a bug in
the program.

Usage of Assert
assert(expression);
Used to check pre-conditions and
post conditions
When expression is false:
Assertion failed: expression, file
filename, line number is displayed.
Then calls abort();
Example

Usage of Assert (Continued)


To test for conditions which are
impossible to occur if the program
works properly.
When is it used:
Introduced during development. (Removes
contradictory assumptions, unexpected
conditions and bad values.)
Compiled out of the code during
production. (assertions degrade system
performance.)

Customizing Assert
As seen assert displays expression,
file filename, line number when
assert fails
What if the reason for its failure is to
be known?
Example

How not to use Assert


Do-not use assert to calculate an entity;
Suppose average marks of a class is to be
calculated and checked.

Usage of asserts should not cause any


change in the data.
Hence assert is called a macro and not a
function.
A function can be called within an assert if
the user is sure that, the called function will
not change any data.

How not to use


Assert(Contd)

Evils of asserts
Assert is active only in debug mode,
hence debug only diagnostic will not
help track problems in production code.
False sense of security
assert(skyDiver->AttachParachute());
skyDiver->JumpFromPlane();

Instead of using asserts some


programmers would prefer explicit error
checking and log tracing.

References
http://www.programmingsimplified.co
m/c/source-code/assert
Writing Solid Code_ Microsoft's
Techniques for Developing Bug-Free
C Programs by Steve Maguire.
Matthew A Telles_ Yuan Hsieh-The
science of debugging-Coriolis (2001)
http://thesoftwarecondition.com/blog
/2010/08/01/cc-assertions-and-defini
ng-your-own-assert-macro/

You might also like