Assert

This is a discussion on Assert within the C++ Programming forums, part of the General Programming Boards category; Question, About Assert, is it basically an if statement?? if extention == "mp3" do this blah blah Assert ( extension ...

  1. #1
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964

    Assert

    Question, About Assert, is it basically an if statement??

    if extention == "mp3"

    do this blah blah

    Assert ( extension == "mp3" )
    do this blah blah blah

    Assert ( extention == "ms3d" )
    do this blahblahbalh

    Is that about it?
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  2. #2
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    It's sort of like an if, because if the condition is false, it throws an assert (stops your program, and a window pops up).

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    It is similar to an if statement, except that is conditionally compiled. That is, an if statement that can magically remove itself from the code.

    http://www.embedded.com/story/OEG20010311S0021
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,295
    No, you should not think of it as an 'if'. Think of it as a call to a function taking a bool parameter. (yes I know it's not quite that either)
    Only use it to check things that should always be true. The example you give is not what an assert should be used for.

    You can't use == like that to compare an array of chars either. You need to use strcmp, or one of the many other flavours of it.

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    52
    Quote Originally Posted by iMalc
    You can't use == like that to compare an array of chars either. You need to use strcmp, or one of the many other flavours of it.
    <quibble> If the variable extention is a std::string, you can do that comparison, because the literal string will be converted implicitly</quibble>

  6. #6
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    I'm thinking of using it to check what extension a string name of type Filename gives me.

    if filename == "foo.mp3"
    std::string extension something syntactically correct
    assert (exstension == "mp3)
    do stuff
    assert (extension == "ms3d")
    do stuff..
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    > I'm thinking of using it to check what extension a string name of type Filename gives me.
    Never use assert() for checking user input.
    1. It is compiled out when you do a release build.
    2. It kills the program in a debug build.
    Neither of these are desirable.

    Assert is for checking program logic, for example
    Code:
    int mySqrt ( int x ) {
      assert( x >= 0 );  /* make sure it's positive! */
    }

  8. #8
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    Ahh, I see..

    Well, it isn't user input... I'm working with an internal resource manager. So assert might still be of use in some situation.
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Ideally, the contents of an assert should never be false. So never put something into an assert that you expect to be false. It is only meant for debugging to ensure that the data you expect is there.

    As has been said, it is compiled out during a release build, so don't do any important calculations or function calls within the assert. For example, the return value of std::set::insert is a pair that has an iterator and a bool. The bool indicates whether the insert succeeded or failed. One might be tempted to assert(mySet.insert(5).second); to ensure the value is not a duplicate. That would be a bad idea because the entire contents of the assert including the insert call would be removed in the release build and the insert would never happen.

    Also, the pseudocode:

    assert (exstension == "mp3)
    do stuff
    assert (extension == "ms3d")
    do stuff..

    is not correct because in that code no matter what one of the asserts will fire (the extension cannot be both "mp3" and "ms3d"). If you are inside a function that only works with mp3 extensions, and you have other code that is supposed to do the error checking to verify that it is mp3, then maybe you would add an assert(extension == "mp3"); into that function because that code always expects to have an mp3 extension.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Assert command
    By Albinoswordfish in forum C Programming
    Replies: 14
    Last Post: 12-24-2008, 08:03 PM
  2. Can you check what is wrong with this code
    By Ron in forum C++ Programming
    Replies: 4
    Last Post: 08-01-2008, 10:59 PM
  3. Customized assert question...
    By Raigne in forum C++ Programming
    Replies: 10
    Last Post: 02-21-2008, 03:28 AM
  4. assert
    By George2 in forum C Programming
    Replies: 1
    Last Post: 10-22-2007, 03:17 AM
  5. How to use assert?
    By jjbuchan in forum C Programming
    Replies: 2
    Last Post: 11-11-2005, 12:40 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21