943,902 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 3094
  • C++ RSS
Mar 27th, 2009
0

Why use assert?

Expand Post »
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).
C++ Syntax (Toggle Plain Text)
  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

C++ Syntax (Toggle Plain Text)
  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
Similar Threads
Featured Poster
Reputation Points: 437
Solved Threads: 204
Posting Virtuoso
daviddoria is offline Offline
1,968 posts
since Feb 2008
Mar 27th, 2009
0

Re: Why use assert?

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 .
Reputation Points: 92
Solved Threads: 20
Posting Whiz
rahul8590 is offline Offline
351 posts
since Mar 2009
Mar 27th, 2009
0

Re: Why use assert?

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.
Reputation Points: 76
Solved Threads: 40
Junior Poster
MrSpigot is offline Offline
156 posts
since Mar 2009
Mar 27th, 2009
0

Re: Why use assert?

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.
Featured Poster
Reputation Points: 437
Solved Threads: 204
Posting Virtuoso
daviddoria is offline Offline
1,968 posts
since Feb 2008
Mar 27th, 2009
0

Re: Why use assert?

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

C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 92
Solved Threads: 20
Posting Whiz
rahul8590 is offline Offline
351 posts
since Mar 2009
Mar 27th, 2009
1

Re: Why use assert?

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Mar 27th, 2009
0

Re: Why use assert?

The real function is
C++ Syntax (Toggle Plain Text)
  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
Featured Poster
Reputation Points: 437
Solved Threads: 204
Posting Virtuoso
daviddoria is offline Offline
1,968 posts
since Feb 2008
Mar 27th, 2009
1

Re: Why use assert?

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Mar 28th, 2009
0

Re: Why use assert?

Click to Expand / Collapse  Quote originally posted by Narue ...
>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!
Reputation Points: 23
Solved Threads: 12
Posting Whiz in Training
n.aggel is offline Offline
202 posts
since Nov 2006
Mar 28th, 2009
0

Re: Why use assert?

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 ...
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Problem with C++ in Visual Studio 2005 under VISTA
Next Thread in C++ Forum Timeline: error: expected unqualified-id before 'bool'





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC