GNU   davin.50webs.com/research
Bringing to you notes for the ages

       Main Menu          Research Projects         Photo Album            Curriculum Vitae      The Greatest Artists
    Email Address       Computer Games          Web Design          Java Training Wheels      The Fly (A Story)   
  Political Activism   Bob Dylan Quotes+       My Life Story          Smoking Cessation          Other Links      
Debugging Macros     String Class I     Linked List System I Java for C Programmers Naming Convention
    String Class II         How I use m4              Strings III                 Symmetrical I/O             Linked Lists II     
Run-Time Type Info   Virtual Methods      An Array System        Science & Religion            Submodes       
  Nested Packages      Memory Leaks    Garbage Collection      Internet & Poverty      What is Knowledge?
Limits of Evolution   Emacs Additions      Function Plotter           Romantic Love        The Next Big Thing
    Science Fiction     Faster Compilation Theory of Morality         Elisp Scoping               Elisp Advice      
  S.O.G.M. Pattern       Safe Properties         School Bullying          Charisma Control          Life and Death    
     Splitting Java          Multiple Ctors       Religious Beliefs         Conversation 1           Conversation 2    
   J.T.W. Language    Emacs Additions II      Build Counter             Relation Plotter          Lisp++ Language  
  Memory Leaks II   Super Constructors CRUD Implementation Order a Website Form There Is An Afterlife
More Occam's Razor C to Java Translator Theory of Morality II


Some macros for debugging C++ programs Free Stuff

Abstract

This article describes some preprocessor macros for use with debugging C++ code. This system has been extended to use my non-standard I/O system described in a later article.

1. Another Version of the C/C++ assert Macro

In addition to the standard C/C++ assert macro I have designed a macro called ASSERT which behaves identically to assert except that it is never turned off (even when the symbol NDEBUG is set). Here is the definition of the macro ASSERT:

#define ASSERT(test)                                           \
   do {                                                        \
   if (!(test)) {                                              \
      PRINTF_AND_EXIT((PLEASE_EMAIL_DAVIN                      \
                      "*** Assertion failed at (%s:%d) \n\n"   \
                      "*** TEST: %s\n",                        \
                      __FILE__,                                \
                      __LINE__,                                \
                      #test));                                 \
   }                                                           \
   } while (0)

As can be seen in the above macro definition, this macro makes use of the following two helper macros. Here is the first:

#ifdef NDEBUG
#define PLEASE_EMAIL_DAVIN "Please email davin dot pearson at gmail dot com with the following information:\n\n"
#else /* !NDEBUG */
#define PLEASE_EMAIL_DAVIN "\n\n"
#endif /* NDEBUG */

and here is the second:

#ifdef NDEBUG
#define PRINTF_AND_EXIT(ARGS_LIST) \
  do { set_gfx_mode(GFX_TEXT,0,0,0,0); allegro_message ARGS_LIST; exit(EXIT_FAILURE); } while (0)
#else /* !NDEBUG */
#define PRINTF_AND_EXIT(ARGS_LIST) \
   do { printf ARGS_LIST; BREAKPOINT; exit(EXIT_FAILURE); } while (0)
#endif /* NDEBUG */


As can be seen in the above macro definition, I use and link with the Allegro graphics library, which includes such functions as set_gfx_mode and allegro_message.

2. Two More Debugging Macros Called ASSERT_INFO and assert_info

I have defined a macro called ASSERT_INFO. Here is the definition of the macro:

#define ASSERT_INFO(test,info)                               \
do {                                                         \
   if (!(test)) {                                            \
      PRINTF_AND_EXIT((PLEASE_EMAIL_DAVIN                    \
                       "*** Assertion failed at (%s:%d)\n\n" \
                       "*** INFO: %s\n",                     \
                       __FILE__,                             \
                       __LINE__,                             \
                       string(info).const_char_star()));     \
   }                                                         \
} while (0)

The advantage of this macro over assert and ASSERT is that there is room for debugging information when the assertion fails. The definition of assert_info is straightforward:

#ifdef NDEBUG
#define assert_info(test,info) ASSERT_INFO(test,info)
#else
#define assert_info(test,info)
#endif /* NDEBUG */

3. A Macro Called SHOULD_NEVER_HAPPEN

The following macro is equivalent to the statement ASSERT(false):

#define SHOULD_NEVER_HAPPEN() ASSERT_INFO(false, "This line should not be executed");

4. A Macro Called ERROR

The following macro is equivalent to the statement ASSERT(false), except that (like ASSERT_INFO) it allows for debugging information to be printed when the assertion fails.

#define ERROR(info) ASSERT_INFO(false, info)

The assert_info macro is roughly equivalent to Java Version 1.5's assertion facility.



Back to Research Projects
This page has the following hit count:
| Main Menu | Research Projects | Photo Album | Curriculum Vitae | The Greatest Artists |
| Email Address | Computer Games | Web Design | Java Training Wheels | The Fly (A Story) |
| Political Activism | Bob Dylan Quotes+ | My Life Story | Smoking Cessation | Other Links |
| Debugging Macros | String Class I | Linked List System I | Java for C Programmers | Naming Convention |
| String Class II | How I use m4 | Strings III | Symmetrical I/O | Linked Lists II |
| Run-Time Type Info | Virtual Methods | An Array System | Science & Religion | Submodes |
| Nested Packages | Memory Leaks | Garbage Collection | Internet & Poverty | What is Knowledge? |
| Limits of Evolution | Emacs Additions | Function Plotter | Romantic Love | The Next Big Thing |
| Science Fiction | Faster Compilation | Theory of Morality | Elisp Scoping | Elisp Advice |
| S.O.G.M. Pattern | Safe Properties | School Bullying | Charisma Control | Life and Death |
| Splitting Java | Multiple Ctors | Religious Beliefs | Conversation 1 | Conversation 2 |
| J.T.W. Language | Emacs Additions II | Build Counter | Relation Plotter | Lisp++ Language |
| Memory Leaks II | Super Constructors | CRUD Implementation | Order a Website Form | There Is An Afterlife |
| More Occam's Razor | C to Java Translator | Theory of Morality II
Last modified: Sun Sep 25 16:11:32 NZDT 2016
Best viewed at 800x600 or above resolution.
© Copyright 1999-2016 Davin Pearson.
Please report any broken links to