dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: what is the point of assert?

  1. #1
    Join Date
    Jul 2011
    Posts
    11

    Unhappy what is the point of assert?

    Hey everyone, I am new to c++ and don't really understand the point of assert. I can code pretty well in Java and understand how to use exceptions, is this like throwing and catching an exception?
    I made a program that divides num1/num2 and I put an assert like;
    assert (num2 != 0);
    but, all it does is report the problem but then still close my program. It doesn't really do anything. I really dont understand the purpose. Can someone help me out a bit? Thanks.

  2. #2
    Join Date
    Aug 2008
    Posts
    902

    Re: what is the point of assert?

    assert is used to report certain types of programming errors during the development stage, since assert does nothing at all in release mode. It's useful because it gives you the file and line number in which the runtime error occurred, allowing you to go in and track down the problem. This only occurs when building debugging versions of your application. When you create a release build, no checks will be made.

  3. #3
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,762

    Re: what is the point of assert?

    An assert is a pre-emptive debugging tool, so to speak. You know some condition must be true, so you assert its truth, but if it turns out that you're wrong, you then discover that you have a bug to fix somewhere before the point of assertion.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: what is the point of assert?

    Don't assert things that may or may not be true. It isn't for validating parameters or input. It's for verifying invariants. If you think a condition must be true, you can assert it. That means if you ever hit the assertion, something unexpected must have happened.

  5. #5
    Join Date
    Jul 2011
    Posts
    11

    Re: what is the point of assert?

    Got it, thanks guys. It makes more sense to me now.

  6. #6
    Join Date
    Mar 2009
    Location
    chennai
    Posts
    7

    Re: what is the point of assert?

    Evaluates an expression and, when the result is false, prints a diagnostic message and aborts the program.

    The assert macro is typically used to identify logic errors during program development by implementing the expression argument to evaluate to false only when the program is operating incorrectly. After debugging is complete, assertion checking can be turned off without modifying the source file by defining the identifier NDEBUG. NDEBUG can be defined with a /D command-line option or with a #define directive. If NDEBUG is defined with #define, the directive must appear before ASSERT.H is included.

    assert prints a diagnostic message when expression evaluates to false (0) and calls abort to terminate program execution. No action is taken if expression is true (nonzero). The diagnostic message includes the failed expression, the name of the source file and line number where the assertion failed.

    In Visual C++ 2005, the diagnostic message is printed in wide characters. Thus, it will work as expected even if there are Unicode characters in the expression.

    The destination of the diagnostic message depends on the type of application that called the routine. Console applications always receive the message through stderr. In a Windows-based application, assert calls the Windows MessageBox function to create a message box to display the message along with an OK button. When the user clicks OK, the program aborts immediately.

    When the application is linked with a debug version of the run-time libraries, assert creates a message box with three buttons: Abort, Retry, and Ignore. If the user clicks Abort, the program aborts immediately. If the user clicks Retry, the debugger is called and the user can debug the program if just-in-time (JIT) debugging is enabled. If the user clicks Ignore, assert continues with its normal execution: creating the message box with the OK button. Note that clicking Ignore when an error condition exists can result in undefined behavior.

    For more information about CRT debugging, see CRT Debugging Techniques.

    The assert routine is available in both the release and debug versions of the C run-time libraries. Two other assertion macros, _ASSERT and _ASSERTE, are also available, but they only evaluate the expressions passed to them when the _DEBUG flag has been defined.
    Last edited by chandrasekaran1987; July 15th, 2011 at 06:20 AM.

  7. #7
    Join Date
    May 2009
    Posts
    2,413

    Re: what is the point of assert?

    Quote Originally Posted by mrmodest View Post
    I can code pretty well in Java
    Java has had an assert statement for years (since 1.4).

    http://java.sun.com/developer/techni...LP/assertions/

    In C++ it's used for exactly the same purpose.
    Last edited by nuzzle; July 16th, 2011 at 09:05 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)