[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Xen-devel] debugging frustration



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@xxxxxxxxxxxxxxxxxxxxx
https://lists.sourceforge.net/lists/listinfo/xen-devel



--
Anthony Liguori
anthony@xxxxxxxxxxxxx



-------------------------------------------------------
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@xxxxxxxxxxxxxxxxxxxxx
https://lists.sourceforge.net/lists/listinfo/xen-devel


 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.