Saturday, May 5, 2007

Bug Masher Tip : The Assert Macro

The Assert is a VC++ (MFC) macro that halts the program execution when its condition fails. This is helpful in debugging applications in particular checking for null values.
A Few Notes:
  • The Assert instruction is only built into an application that is meant for debug. In other words should you build your exe as a release version your assert won't get built into the binary.
  • The Assert instruction is meant to test logical errors, basically its meant for debugging run time errors
Example code

void checkString(LPSTR lpsParam){
//Checks to make sure the parameter isn't null
Assert(lpsParam!=NULL);
...
}

Check out the MSDN article here for a more detailed explanation.

No comments: