Skip to main content
GameDev.net gamedev.net
🔒 Locked

Assert not working

Started by Scheermesje Oct 20, 2003 at 9:34 AM 5 replies 700+ views
Original Post
Scheermesje
Scheermesje
Hi I''ve got the following code:
CVector3D& CVector3D::operator/=(float fl)	
{
	assert(fl != 0);
	
	float fl = 1.0f/fl;
	x *= fl;
	y *= fl;
	z *= fl;
	return *this;
}
If I call this function with fl = 0 it should create an assert error and stop the programm.. but it''s just doing nothing. How''s that possible?
Sabbac
Sabbac
The only thing I can think of is that your not compiling in debug mode. Most of the time assert(...) gets #define''d to oblivion if _DEBUG isn''t defined...
Scheermesje
Scheermesje
Mmm.. I was compiling in release mode.. in Debug it''s working

But according to the help:

The assert routine is available in both the release and debug versions of the C run-time libraries.

Is the problem that I''ve created a C++ file? Or something else?
Sneftel
Sneftel
This is the way assert() is supposed to work. The idea is to test it in Debug mode until you are certain that assert() will never ever ever be called with a false value. Then you compile it in release mode, so you don''t spend any time checking.


How appropriate. You fight like a cow.
dmikesell
dmikesell
If you want assertions to fire in release code, you can write your own version that doesn''t dump core but just prints a message:


void MyAssert(char const * file, unsigned line)
{
fprintf(stderr, "Assertion failed at line %u in file %s\n", line, file);
fflush(NULL);
}
.
.
#define ASSERT(expr) if (!(expr)) MyAssert(__FILE__, __LINE__);


Now just call your custom ASSERT macro in your code. You could add an "#ifndef NDEBUG" check to MyAssert to dump core in debug mode if you like.


--
Dave Mikesell Software & Consulting
Rock2000
Rock2000
But also according to the help:

assertion checking can be turned off without modifying the source file by defining the identifier NDEBUG

If you check your project settings in VC, Release mode has NDEBUG defined. That''s the default behavior. I''ve read an article recently that advocated leaving asserts in the release build, but I don''t subcribe to that yet. At least not in video games. It''ll make fixing user reported bugs easier though.
Scheermesje
Scheermesje
Thnx for the help guys I''ve learned something

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.