Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. A question about Assert function

A question about Assert function

Scheduled Pinned Locked Moved C / C++ / MFC
questiondebugginghelptutorialannouncement
18 Posts 8 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • O Obsidianhom

    Hi, I know Assert function can raise a break in Debug mode. It helps a lot. but when it is in Release mode,the Compile ignore Aseert function. then how can I know it meets a error? for example: int open_file(char * filename) { FILE *fp; fp=fopen(filename,"wr"); //p1 assert(fp!=NULL); //p2 //do something //p3 } in Debug mode: when p1 fail, fp will be NULL and the assert will raise a break,then it exit. in Release mode: the Compile ignore the assert function, what should I do if fopen failed? if it will go on running statements after p3 ? to avoid this happend, should I have to replace all the assert statements in Release mode? thank you.

    C Offline
    C Offline
    Cedric Moonen
    wrote on last edited by
    #2

    You are not using the assert properly. This function should be used only to test the code. An assert should never be reached in a released product. Assert is used to detect errors in your code, not detect a file which is missing. In your case, if the file doesn't not exist you should handle the error in a better way. For instance your function could return an error code or throw an exception which has to be handled at a higher level.

    Cédric Moonen Software developer
    Charting control [v2.0 - Updated] OpenGL game tutorial in C++

    C O 2 Replies Last reply
    0
    • O Obsidianhom

      Hi, I know Assert function can raise a break in Debug mode. It helps a lot. but when it is in Release mode,the Compile ignore Aseert function. then how can I know it meets a error? for example: int open_file(char * filename) { FILE *fp; fp=fopen(filename,"wr"); //p1 assert(fp!=NULL); //p2 //do something //p3 } in Debug mode: when p1 fail, fp will be NULL and the assert will raise a break,then it exit. in Release mode: the Compile ignore the assert function, what should I do if fopen failed? if it will go on running statements after p3 ? to avoid this happend, should I have to replace all the assert statements in Release mode? thank you.

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #3

      I concur with Cédric. Use assert to discover logic flaws in your program while debugging, don't use it to detect exceptional circumstances, things that go wrong not because there is an error in your program, but because the circumstances are against you, as in a missing file. Hence:

      // EDIT
      #define null NULL
      // /EDIT

      if (fp==null) {
      // whatever needs to happen when the file won't open
      } else {
      // whatever you want to do with the file
      }

      :)

      Luc Pattyn [Forum Guidelines] [My Articles]


      Avoiding unwanted divs (as in "articles needing approval") with the help of this FireFox add-in


      modified on Friday, April 17, 2009 2:59 PM

      C 1 Reply Last reply
      0
      • L Luc Pattyn

        I concur with Cédric. Use assert to discover logic flaws in your program while debugging, don't use it to detect exceptional circumstances, things that go wrong not because there is an error in your program, but because the circumstances are against you, as in a missing file. Hence:

        // EDIT
        #define null NULL
        // /EDIT

        if (fp==null) {
        // whatever needs to happen when the file won't open
        } else {
        // whatever you want to do with the file
        }

        :)

        Luc Pattyn [Forum Guidelines] [My Articles]


        Avoiding unwanted divs (as in "articles needing approval") with the help of this FireFox add-in


        modified on Friday, April 17, 2009 2:59 PM

        C Offline
        C Offline
        CPallini
        wrote on last edited by
        #4

        Please no C#ing here. ;P :)

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
        [My articles]

        L 1 Reply Last reply
        0
        • C CPallini

          Please no C#ing here. ;P :)

          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
          This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
          [My articles]

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #5

          sorry, I meant NULL of course. Or ZERO, ZILCH, RIENDUTOUT, NADA, NIENTE, whatever you fancy. :)

          Luc Pattyn [Forum Guidelines] [My Articles]


          Avoiding unwanted divs (as in "articles needing approval") with the help of this FireFox add-in


          C 1 Reply Last reply
          0
          • L Luc Pattyn

            sorry, I meant NULL of course. Or ZERO, ZILCH, RIENDUTOUT, NADA, NIENTE, whatever you fancy. :)

            Luc Pattyn [Forum Guidelines] [My Articles]


            Avoiding unwanted divs (as in "articles needing approval") with the help of this FireFox add-in


            C Offline
            C Offline
            CPallini
            wrote on last edited by
            #6

            Well I see you agree with me (and Rajesh, I suppose): C/C++'s NULL is more imperative than C# cheap fake one. :-D

            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
            This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
            [My articles]

            R 1 Reply Last reply
            0
            • C CPallini

              Well I see you agree with me (and Rajesh, I suppose): C/C++'s NULL is more imperative than C# cheap fake one. :-D

              If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
              This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
              [My articles]

              R Offline
              R Offline
              Rajesh R Subramanian
              wrote on last edited by
              #7

              Hear hear... :-D

              It is a crappy thing, but it's life -^ Carlo Pallini

              1 Reply Last reply
              0
              • C Cedric Moonen

                You are not using the assert properly. This function should be used only to test the code. An assert should never be reached in a released product. Assert is used to detect errors in your code, not detect a file which is missing. In your case, if the file doesn't not exist you should handle the error in a better way. For instance your function could return an error code or throw an exception which has to be handled at a higher level.

                Cédric Moonen Software developer
                Charting control [v2.0 - Updated] OpenGL game tutorial in C++

                C Offline
                C Offline
                China_Kevin
                wrote on last edited by
                #8

                强烈同意楼上~~~~~

                C D 2 Replies Last reply
                0
                • C China_Kevin

                  强烈同意楼上~~~~~

                  C Offline
                  C Offline
                  Cedric Moonen
                  wrote on last edited by
                  #9

                  China_Kevin wrote:

                  强烈同意楼上~~~~~

                  Uh, what ?? :confused:

                  Cédric Moonen Software developer
                  Charting control [v2.0 - Updated] OpenGL game tutorial in C++

                  C 2 Replies Last reply
                  0
                  • C Cedric Moonen

                    China_Kevin wrote:

                    强烈同意楼上~~~~~

                    Uh, what ?? :confused:

                    Cédric Moonen Software developer
                    Charting control [v2.0 - Updated] OpenGL game tutorial in C++

                    C Offline
                    C Offline
                    China_Kevin
                    wrote on last edited by
                    #10

                    I am Chinese,HOHO, 强烈同意楼上~~~~~ just means I agree with you very much...

                    1 Reply Last reply
                    0
                    • C Cedric Moonen

                      China_Kevin wrote:

                      强烈同意楼上~~~~~

                      Uh, what ?? :confused:

                      Cédric Moonen Software developer
                      Charting control [v2.0 - Updated] OpenGL game tutorial in C++

                      C Offline
                      C Offline
                      China_Kevin
                      wrote on last edited by
                      #11

                      your name "Cedric" why your "C" is brighter than my name "China_Kevin" "C"

                      R D 2 Replies Last reply
                      0
                      • C China_Kevin

                        your name "Cedric" why your "C" is brighter than my name "China_Kevin" "C"

                        R Offline
                        R Offline
                        Rajesh R Subramanian
                        wrote on last edited by
                        #12

                        Because he is an MVP.

                        It is a crappy thing, but it's life -^ Carlo Pallini

                        C 1 Reply Last reply
                        0
                        • R Rajesh R Subramanian

                          Because he is an MVP.

                          It is a crappy thing, but it's life -^ Carlo Pallini

                          C Offline
                          C Offline
                          China_Kevin
                          wrote on last edited by
                          #13

                          He is a valuable man?

                          1 Reply Last reply
                          0
                          • C Cedric Moonen

                            You are not using the assert properly. This function should be used only to test the code. An assert should never be reached in a released product. Assert is used to detect errors in your code, not detect a file which is missing. In your case, if the file doesn't not exist you should handle the error in a better way. For instance your function could return an error code or throw an exception which has to be handled at a higher level.

                            Cédric Moonen Software developer
                            Charting control [v2.0 - Updated] OpenGL game tutorial in C++

                            O Offline
                            O Offline
                            Obsidianhom
                            wrote on last edited by
                            #14

                            I got the idea and I known how to do it. thank you both Luc Pattyn and Cédric Moonen~ :) :)

                            C 1 Reply Last reply
                            0
                            • O Obsidianhom

                              I got the idea and I known how to do it. thank you both Luc Pattyn and Cédric Moonen~ :) :)

                              C Offline
                              C Offline
                              China_Kevin
                              wrote on last edited by
                              #15

                              why do not think me~~~~~o(∩_∩)o...哈哈 ;P

                              1 Reply Last reply
                              0
                              • C China_Kevin

                                your name "Cedric" why your "C" is brighter than my name "China_Kevin" "C"

                                D Offline
                                D Offline
                                David Crow
                                wrote on last edited by
                                #16

                                I hereby bestow on you a "brighter" name: China_Kevin Carry it with honor.

                                "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

                                "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                                C 1 Reply Last reply
                                0
                                • D David Crow

                                  I hereby bestow on you a "brighter" name: China_Kevin Carry it with honor.

                                  "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

                                  "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                                  C Offline
                                  C Offline
                                  China_Kevin
                                  wrote on last edited by
                                  #17

                                  You lie to me~~~~~ That a walloping huge lie!! :(( :(( :((

                                  1 Reply Last reply
                                  0
                                  • C China_Kevin

                                    强烈同意楼上~~~~~

                                    D Offline
                                    D Offline
                                    dandanfeng160
                                    wrote on last edited by
                                    #18

                                    Me too~ :laugh:

                                    1 Reply Last reply
                                    0
                                    Reply
                                    • Reply as topic
                                    Log in to reply
                                    • Oldest to Newest
                                    • Newest to Oldest
                                    • Most Votes


                                    • Login

                                    • Don't have an account? Register

                                    • Login or register to search.
                                    • First post
                                      Last post
                                    0
                                    • Categories
                                    • Recent
                                    • Tags
                                    • Popular
                                    • World
                                    • Users
                                    • Groups