A question about Assert function
-
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.
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
// /EDITif (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
-
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
// /EDITif (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
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] -
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]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
-
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
Well I see you agree with me (and Rajesh, I suppose):
C/C++
'sNULL
is more imperative thanC#
cheap fake one. :-DIf 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] -
Well I see you agree with me (and Rajesh, I suppose):
C/C++
'sNULL
is more imperative thanC#
cheap fake one. :-DIf 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]Hear hear... :-D
It is a crappy thing, but it's life -^ Carlo Pallini
-
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++强烈同意楼上~~~~~
-
强烈同意楼上~~~~~
China_Kevin wrote:
强烈同意楼上~~~~~
Uh, what ?? :confused:
Cédric Moonen Software developer
Charting control [v2.0 - Updated] OpenGL game tutorial in C++ -
China_Kevin wrote:
强烈同意楼上~~~~~
Uh, what ?? :confused:
Cédric Moonen Software developer
Charting control [v2.0 - Updated] OpenGL game tutorial in C++I am Chinese,HOHO, 强烈同意楼上~~~~~ just means I agree with you very much...
-
China_Kevin wrote:
强烈同意楼上~~~~~
Uh, what ?? :confused:
Cédric Moonen Software developer
Charting control [v2.0 - Updated] OpenGL game tutorial in C++your name "Cedric" why your "C" is brighter than my name "China_Kevin" "C"
-
your name "Cedric" why your "C" is brighter than my name "China_Kevin" "C"
Because he is an MVP.
It is a crappy thing, but it's life -^ Carlo Pallini
-
Because he is an MVP.
It is a crappy thing, but it's life -^ Carlo Pallini
He is a valuable man?
-
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++I got the idea and I known how to do it. thank you both Luc Pattyn and Cédric Moonen~ :) :)
-
I got the idea and I known how to do it. thank you both Luc Pattyn and Cédric Moonen~ :) :)
why do not think me~~~~~o(∩_∩)o...哈哈 ;P
-
your name "Cedric" why your "C" is brighter than my name "China_Kevin" "C"
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
-
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
You lie to me~~~~~ That a walloping huge lie!! :(( :(( :((
-
强烈同意楼上~~~~~
Me too~ :laugh: