Why use assert?

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Feb 2008
Posts: 629
Reputation: daviddoria is a jewel in the rough daviddoria is a jewel in the rough daviddoria is a jewel in the rough 
Solved Threads: 46
daviddoria daviddoria is offline Offline
Practically a Master Poster

Why use assert?

 
0
  #1
Mar 27th, 2009
For a while now I've been doing this when I want to check the input of a function (in this example, to make sure they are not equal).
  1. void foo(int a, int b)
  2. {
  3. assert(a != b);
  4. }

But when the assert fails, I always find my self changing it to

  1. void foo(int a, int b)
  2. {
  3. if(a != b)
  4. {
  5. cout << "a: " << a << " b: " << b << endl;
  6. exit(0);
  7. }
  8. }

Is there a reason that I shouldn't just do this in the first place?

Thanks,
Dave
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 210
Reputation: rahul8590 is on a distinguished road 
Solved Threads: 11
rahul8590's Avatar
rahul8590 rahul8590 is offline Offline
Posting Whiz in Training

Re: Why use assert?

 
0
  #2
Mar 27th, 2009
Well assert is a macro that expands to if statement .
if the test value is zero assert prints the message to stderr:

if the test fails :
test , filename , line linenum is printed .
linenum is the line where the macro is used

well it depends on a persons usage that were exactly he has to use.
if u want to check some no (which might be a passwd) then use assert function . cuz if it fails the program is aborted.
but u want to perform conventional check then its prudent to use the if statements .
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 141
Reputation: MrSpigot is on a distinguished road 
Solved Threads: 33
MrSpigot's Avatar
MrSpigot MrSpigot is offline Offline
Junior Poster

Re: Why use assert?

 
0
  #3
Mar 27th, 2009
The important difference with assert() is that it doesn't affect production (release build) code. So all those checks don't affect performance, and as the code is debugged by then, you don't need them right? (yeah right).
For debugging I find assert() much better than exit(0) as it leaves the debugger active for a good old poke about.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 629
Reputation: daviddoria is a jewel in the rough daviddoria is a jewel in the rough daviddoria is a jewel in the rough 
Solved Threads: 46
daviddoria daviddoria is offline Offline
Practically a Master Poster

Re: Why use assert?

 
0
  #4
Mar 27th, 2009
ah good idea with asser() instead of exit(0), I always wish that it didn't exit (even though I told it to exit !? haha) for exactly that reason.

Thanks for the comments.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 210
Reputation: rahul8590 is on a distinguished road 
Solved Threads: 11
rahul8590's Avatar
rahul8590 rahul8590 is offline Offline
Posting Whiz in Training

Re: Why use assert?

 
0
  #5
Mar 27th, 2009
Well correct me if i am wrong .
asser macro aborts from the program not exits . there is a subtle difference btw them . the former i guess exits without performing any task . but the latter i guess has a safer exit.


well if u want to abstain ur self from exit i guess u can turn ur code something like this

  1.  
  2. int cmp (int a , int b)
  3. {
  4. return(a!=b);
  5. }
  6.  
  7. and in main function
  8. if(cmp(a,b))
  9. cout<<"both r not equal";

i guess this should work
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,660
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 723
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Why use assert?

 
1
  #6
Mar 27th, 2009
>Is there a reason that I shouldn't just do this in the first place?
It depends on your debugging needs. If the assertion is absolutely critical for the application to continue, and the information from a failed assert is sufficient, by all means use assert. On the other hand, if it's not a fatal error, you can usually log/trace and continue on. On the third hand, a lot of people replace the standard assert with their own macro that provides more helpful information.

Also remember that assert is meant to check code integrity. You're saying "This should always be true, and I'm writing code based on that assumption. If the assumption is false, the code is broken and this assert is there to keep the bug from making it to release". If the assertion fails, it's a logic bug that needs to be fixed.

I'd wager that instead of asserting that a and b will never be equal, you should be testing this as a case at runtime and producing a suitable diagnostic (ie. return value, setting errno, etc...). Otherwise, I'd immediately ask you how you know that a and b will never be equal. Are you generating those values? Do you have an explicit runtime check surrounding each call to this function?

Your example smells funny, and that makes your question suspect as well.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 629
Reputation: daviddoria is a jewel in the rough daviddoria is a jewel in the rough daviddoria is a jewel in the rough 
Solved Threads: 46
daviddoria daviddoria is offline Offline
Practically a Master Poster

Re: Why use assert?

 
0
  #7
Mar 27th, 2009
The real function is
  1. double AverageDistance(const vector<Point> &A, const vector<Point> &B)
  2. {
  3. assert(A.size() == B.size());
  4. double Total = 0;
  5. for(unsigned int i = 0; i < A.size(); i++)
  6. Total+= Distance(A[i], B[i]);
  7.  
  8. return Total/A.size();
  9. }

as you can see, it doesn't make sense to get the average distance of two vectors of correspondences with different lengths! So this should indeed be handled with an assert, while some things may not.

Narue: What did you mean by "log/trace"?

Dave
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,660
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 723
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Why use assert?

 
1
  #8
Mar 27th, 2009
>So this should indeed be handled with an assert
Once again it depends. If you have absolute control over the size of both vectors in the code, then an assertion is warranted because a mismatch is a logic error. If you don't have control over the size (it's based on user input, for example) then an assertion is a mistake. In the latter case I would prefer throwing an exception.

>Narue: What did you mean by "log/trace"?
Don't scuttle the boat if you can fix the problem. The majority of errors aren't fatal, and recovery is an option. But silent recovery may or may not be a good idea. In cases where you want to notify users of problems without throwing something in their face, you can write to an execution or error log that can be viewed at a later time.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 202
Reputation: n.aggel is an unknown quantity at this point 
Solved Threads: 11
n.aggel's Avatar
n.aggel n.aggel is offline Offline
Posting Whiz in Training

Re: Why use assert?

 
0
  #9
Mar 28th, 2009
Originally Posted by Narue View Post
>So this should indeed be handled with an assert
Once again it depends. If you have absolute control over the size of both vectors in the code, then an assertion is warranted because a mismatch is a logic error. If you don't have control over the size (it's based on user input, for example) then an assertion is a mistake. In the latter case I would prefer throwing an exception.
The cleanest and nicest explanation, one could hope for!
Two roads diverged in a wood, and I— I took the one less traveled by, and that has made all the difference.

by Robert Frost the "The Road Not Taken"
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: Why use assert?

 
0
  #10
Mar 28th, 2009
Assert is a C++ macro and when the compiler comes across assert, it replaces the assert call with the instructions to evaluate an expression + the instructions to exit the program if the expression returned false ...

e.g.: assert(1>2); will exit the program because 1 isn't greater than 2 ...
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC