do {...} while (0)

Jerry Feldman gaf at blu.org
Sat Feb 7 10:27:22 EST 2009


On 02/07/2009 09:56 AM, Michael ODonnell wrote:
>   
>> But if you change the macro to:
>> #define b0rken(x) ({return x;})
>> you will find that the compiler likes it again.
>>     
>
> I mistakenly interpreted your statement that the two constructions
> are equivalent:
>
>   
>> What I see confufsing is:
>> do { ... } while(0);
>>     
> 		[...]
>   
>> { ... } would be equivalent to above
>>     
>
>
> ...to mean that you believe that they are generally interchangeable.
> Since that do-while(0) trick is in fairly common usage (often inside
> macros) I just wanted to illustrate a situation where they're not.
>   
In the kernel code you presented, it would be functionally equivalent, 
but not syntactically or semantically. The bottom line is that in the 
section of code you presented, what was important was to establish a 
block so a variable could be defined. Adding the do <statement> while(0) 
is just adding some extraneous code that would be most probably 
optimized out, but even if it is not, it is in an error condition.

-- 
Jerry Feldman <gaf at blu.org>
Boston Linux and Unix
PGP key id: 537C5846
PGP Key fingerprint: 3D1B 8377 A3C0 A5F2 ECBB  CA3B 4607 4319 537C 5846


-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 251 bytes
Desc: OpenPGP digital signature
Url : http://mail.gnhlug.org/mailman/private/gnhlug-discuss/attachments/20090207/bdd0c9bc/attachment.bin 


More information about the gnhlug-discuss mailing list