Question:
what is the meaning of this c++ sentence?
?
2010-02-13 15:11:43 UTC
"If you convert an assertion
to a Boolean expression, then the predefined macro assert can be used to check
whether or not your code satisfies the assertion."

my enlglish is not good, can someboy expalin it to me?thanks.
Four answers:
Mantis
2010-02-13 15:38:54 UTC
An assertion is something you expect to be true. You expect this to be true so much that if it's not, it's likely an urecoverable error and the program should terminate (although exactly what happens is frequently up to the programmer). Asserts are often eliminated from release code using a compiler switch, which means the check only happens when you're writing the program (so you don't have needless checks slowing you down).



This is seperate from normal error checking. If you try to connect to a database or open a file and that fails, that's a normal and expected error. If you ask the user to enter a number or a salary and they enter "boogity boogity", that's an expected error you should check for.



Let's say, on the other hand, you write a function like this:



double CalculateAverage(double * array, int arrayLength)

{

}



Inside this function you might write "Assert(arrayLength > 0)" and "Assert(array != NULL). This isn't something that should ever happen, and if it does you're probably going to get a divide by zero error or other crash anyway. You can't just return a zero here (zero could really be the average, after all) and the calling function probably isn't checking for it anyway. These conditions would only be true if there is a logic bug elsewhere in your program. If you design your CalculateAverage function to be crash-safe and never crash if bad data comes in, all you're doing is hiding your bug. Instead, a good Assert here can help you find the bug. Instead of a random "An error occurred in this application" message you'll get a specific "Assertion failed: arrayLength > 0" which can help you immediately see what happened.



So, that's what an assert is. You can convert any boolean error check (ie, "a == 3", "x > 5", "y != 0", etc.) into an assert. Assertions should *not* replace normal error checking, but only errors that can't be handled.



(Okay, you *could* rewrite that average function to handle the error. So any error *can* be handled. But this is a case where the data should be validated long before that function is called.)



Good luck.
Steve
2010-02-13 15:20:59 UTC
You have a statement (an assertion) lets say x > 5 then you can use a macro (a already defined code) to test whether x > 5 is true or not. If it is true it satisfies the assertion, if it is false it does not.
MoldyLemonMedia.com
2010-02-13 15:15:33 UTC
All I know is that boolean means a true/false value
hoch
2016-11-06 11:16:27 UTC
In Roman numerals, C = a hundred, so a 'c notice' is one for $a hundred. i do no longer understand your chum, till he capacity 'center C notice' it is chanced on interior the middle (center) of a piano keyboard.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...