[Home]  [Edit this page]  [Recent Changes]  [Special Pages]  [Help
CAssert

(C) Assert

A standard  ?debugging macro.

Use assert to
  • Document internal assumptions and invariants
  • Shorten the time finding bugs
  • Improve productivity
To use it #include the header file assert.

There is also the possibility of using a compile time assert statements.

Page overview

  • Shorten the time finding bugs
  • Improve productivity
  • Short trick
  • Make your own assertstatement
  • 'Assert' links
  • Code links
  • References

Shorten the time finding  ?bugs

It's hard enough to find an error in your code when you're looking for it; it's even harder when you've assumed your code is error-free
Steve McConnell


assert is a kind of if-statement, which can be cut out by the  ?preprocessor when your program is safe.

Use assert to document internal assumptions and invariants. Use assertions to verify preconditions and postconditions. Use assertions for conditions that should never occur.

  1. include <assert.h>
int main() { int population = 1600; int malePopulation = -1; double fractionMales = getFraction(); assert(fractionMales>=0.0 && fractionMales<=1.0); int malePopulation = fractionMales * (double)population; }


In above program, the function getFraction() is called to get a fraction. In this case, the fraction can only have values from 0.0 to and including 1.0. Therefore, you assume that getFraction produces a value in this range. If this is false, you will get an error message, immediatly showing you where you were false in your assumptions.

But what is the difference with writing the code below?
  if (fractionMales<=0.0 && fractionMales>=1.0) { printf("Error!"); exit(1); }


The crucial difference is that when your program is debugged in total, you do not need to comment out all these if-statements.

  1. define NDEBUG
  2. include <assert.h>
int main() { assert(2==3); /* Of course, this is not true */ }


The code above contains a false assumption. But when  ?NDEBUG is  ?#defined, the preprocessor removes the assert statement for you.

Improved productivity

If you have written a new function, you do not need to check it in a seperate program anymore:

char *  getFileNameExtension(const char * fileName)
{
  //Some way of doing this
}
int main()
{
  const char * fileName = getSomeFileName();
  const char * extension = getFileNameExtension(fileName);
  assert(strcmp(getFileNameExtension("Test.txt"),"txt")==0);
}


Short trick

If you have an  ?exception, you can show it on screen WITH line number using assert:
assert(!"This is due to: ");
As !"Something" is always  ?false (because "Something" is always  ?true).

Make your own assert statement

Below is an example how to make your own assert statement.

//TODO: CONVERT THIS TO PRINTF STATEMENTS
  1. ifdef NDEBUG
      1. define assert(x) ((void)0)
  2. else
      1. define assert(x) \
if (! (x)) \ { \ std::cout << "ERROR!! Assertion " << #x << " failed\n"; \ std::cout << " on line " << __LINE__ << "\n"; \ std::cout << " in file " << __FILE__ << "\n"; \ }
  1. endif


'Assert' links

Code links



last edited (November 24, 2006) by bilderbikkel, Number of views: 2090, Current Rev: 4 (Diff)

[Edit this page]  [Page history]  [What links here]  [Discuss this topic]  [Printer Friendly]  

Members

Username:

Password:


Register
Forgot Password?




Programmers Heaven - for .NET, Java, C/C++ and WEB Developers!
© 1996-2008 Community Networks Ltd. All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited. Violators of this policy may be subject to legal action. Please read Terms Of Use and Privacy Statement for more information. Development by Tore Nestenius at .NET Consultant - Synchron Data.