Skip to content

Instantly share code, notes, and snippets.

@dylnuge
Created September 22, 2012 04:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dylnuge/3765106 to your computer and use it in GitHub Desktop.
Save dylnuge/3765106 to your computer and use it in GitHub Desktop.
Debugging Macros for C
// Define debugging macros. Because actual code shouldn't be littered with
// debugging printfs and sanity check assertations.
#ifdef DEBUG
#define debug_assert(...) assert(__VA_ARGS__)
#define debug_printf(...) printf(__VA_ARGS__)
#else
#define debug_assert(...) ;
#define debug_printf(...) ;
#endif
@dylnuge
Copy link
Author

dylnuge commented Sep 22, 2012

Based on @kejistan's work. Modifications to have an assert as well as printf (leaving assert in prod code does not change functionality but may affect performance) and to make the NOP more clear (a single ; will work and will be unused by the compiler regardless of optimization flags).

@dylnuge
Copy link
Author

dylnuge commented Sep 22, 2012

Some notes: debug_assert is useful for some assert functions, including the one I'm using in my malloc code. It's probably not needed if you're using <assert.h>, since if NDEBUG is defined before assert.h is included, the assert function is replaced with a NOP.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment