[Home]
[Edit this page]
[Recent Changes]
[Special Pages]
[Help]
CAssert
Use assert to
There is also the possibility of using a compile time assert statements.
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.
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?
The crucial difference is that when your program is debugged in total, you do not need to comment out all these if-statements.
The code above contains a false assumption. But when ?NDEBUG is ?#defined, the preprocessor removes the assert statement for you.
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
CAssert
(C) Assert
A standard ?debugging macro.Use assert to
- Document internal assumptions and invariants
- Shorten the time finding bugs
- Improve productivity
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.
int main() { int population = 1600; int malePopulation = -1; double fractionMales = getFraction(); assert(fractionMales>=0.0 && fractionMales<=1.0); int malePopulation = fractionMales * (double)population; }
- include <assert.h>
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.
int main() { assert(2==3); /* Of course, this is not true */ }
- define NDEBUG
- include <assert.h>
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 STATEMENTSif (! (x)) \ { \ std::cout << "ERROR!! Assertion " << #x << " failed\n"; \ std::cout << " on line " << __LINE__ << "\n"; \ std::cout << " in file " << __FILE__ << "\n"; \ }
- ifdef NDEBUG
- define assert(x) ((void)0)
- else
- define assert(x) \
- endif
'Assert' links
Code links
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
