Thread: Debugging question

  1. #1
    Darkness Prevails Dark_Phoenix's Avatar
    Join Date
    Oct 2006
    Location
    Houston, Texas
    Posts
    174

    Debugging question

    if I have the following function
    Code:
    inline void SHOWERROR(char *str)
    {
        #ifdef _DEBUG
        char buf[1024];
        char file[] = __FILE__;
        int line = __LINE__;
        sprintf(buf, "File: %s\nLine: %d\n%s", file, line, str);
        MessageBox(0, buf, "Error", 0);
        #endif
    }
    Assuming _DEBUG was not defined, would the compiler generate any extra instruction in this if statement?
    Code:
        if (!(memcpy(pDest, pSource, size) ) )
        {
            SHOWERROR("Error Message");
        }
    or would it be better to use something like this
    Code:
    #define SHOWERROR(s)   { char buf[1024];                                      \
                             char file[] = __FILE__;                              \
                             int line = __LINE__;                                 \
                             sprintf(buf, "File: %s\nLine: %d\n%s",file,line,s);  \
                             MessageBox(0, buf, "Error", 0); }
    ...
        if (!(memcpy(pDest, pSource, size) ) )
        {
            #ifdef _DEBUG
            SHOWERROR("Error Message");
            #endif
        }
    Using Code::Blocks and Windows XP

    In every hero, there COULD be a villain!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    An empty inline function should be completely eliminated by the compiler.
    Your first approach seems fine to me.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    I'm not sure if the compiler eliminates it or not, but I usually write debug functions like this:

    Code:
    #ifdef _DEBUG
        void DBG_FUNC( int param )
        {
            ...
        }
    #else
    #   define DBG_FUNC( param )
    #endif

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dev-C++: Problems with Breakpoint Debugging
    By Thileepan_Bala in forum C Programming
    Replies: 1
    Last Post: 01-17-2008, 10:48 AM
  2. Debugging book recommendation
    By dagans in forum Projects and Job Recruitment
    Replies: 1
    Last Post: 09-13-2005, 07:35 PM
  3. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  4. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM
  5. Debugging Question
    By Unregistered in forum C++ Programming
    Replies: 0
    Last Post: 12-19-2001, 05:42 PM