All of lore.kernel.org
 help / color / mirror / Atom feed
From: Anthony Liguori <anthony@codemonkey.ws>
To: Mark Williamson <maw48@cl.cam.ac.uk>
Cc: xen-devel@lists.sourceforge.net, Arthur Bergman <abergman@fotango.com>
Subject: Re: debugging frustration
Date: Mon, 14 Feb 2005 09:03:05 -0600	[thread overview]
Message-ID: <4210BDA9.5070901@codemonkey.ws> (raw)
In-Reply-To: <200502141424.38935.maw48@cl.cam.ac.uk>

Mark Williamson wrote:

>>Also, is there a reason that not having DEBUG in turns dprintf into do
>>{} while(0) instead of just turning into ""
>>    
>>
The do {} while (0) idiom is commonly used in macros to protect against 
the following.  Say you had a macro that looked like this:

#define DEBUG(a, b)  if (a > debug_level) printf(b);

If you called it like this:

if (ptr == NULL)
   DEBUG(10, "Bad pointer");
else
   *ptr = 2;

What would actually happen is that the else clause would get attached to 
the if from the DEBUG() statement and you would get very odd behavior.

Using do {} while (0) is just the common solution to this problem.  It's 
not the only solution, but it's what most people use.

Regards,
Anthony Liguori

>>It makes the macro behave more like a proper statement.  e.g.  If we try to 
>>do:
>>
>>if ( 1 == 2 debug_print("foo!") )
>>{
>>
>>}
>>
>>It won't compile, even if debugging is switched off.  If we just did "#define 
>>debug_print(...)" then this would compile and we'd only notice the error when 
>>debugging was switched on.
>>
>>That's a quite contrived example but basically it keeps the developers 
>>honest ;-)
>>
>>Cheers,
>>Mark
>>
>>
>>-------------------------------------------------------
>>SF email is sponsored by - The IT Product Guide
>>Read honest & candid reviews on hundreds of IT Products from real users.
>>Discover which products truly live up to the hype. Start reading now.
>>http://ads.osdn.com/?ad_id=6595&alloc_id=14396&op=click
>>_______________________________________________
>>Xen-devel mailing list
>>Xen-devel@lists.sourceforge.net
>>https://lists.sourceforge.net/lists/listinfo/xen-devel
>>
>>    
>>


-- 
Anthony Liguori
anthony@codemonkey.ws



-------------------------------------------------------
SF email is sponsored by - The IT Product Guide
Read honest & candid reviews on hundreds of IT Products from real users.
Discover which products truly live up to the hype. Start reading now.
http://ads.osdn.com/?ad_id=6595&alloc_id=14396&op=click

  reply	other threads:[~2005-02-14 15:03 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-02-14 14:20 debugging frustration Arthur Bergman
2005-02-14 14:24 ` Mark Williamson
2005-02-14 15:03   ` Anthony Liguori [this message]
2005-02-14 16:14     ` Luciano Miguel Ferreira Rocha
2005-02-14 16:38       ` Mark Williamson
2005-02-14 16:41       ` Anthony Liguori

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4210BDA9.5070901@codemonkey.ws \
    --to=anthony@codemonkey.ws \
    --cc=abergman@fotango.com \
    --cc=maw48@cl.cam.ac.uk \
    --cc=xen-devel@lists.sourceforge.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.