Werid <sys/queue.h> - it has useless do{} while(0)
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > Werid <sys/queue.h> - it has useless do{} while(0)




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    Werid <sys/queue.h> - it has useless do{} while(0)  
djhong@gmail.com


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
04-26-07 06:17 PM

(Since queue.h is not a part of standard C, I'd lke to ask here)

For example, BSD implementation of sys/queue.h  has following weird
macros:
I can't understand why   "do{}while(0)"  is used...      Removing
"do{}while(0)" from these macros should be ok. Right?


#define	SLIST_INIT(head) do {						\
SLIST_FIRST((head)) = NULL;					\
} while (0)

#define	SLIST_INSERT_AFTER(slistelm, elm, field) do {			\
SLIST_NEXT((elm), field) = SLIST_NEXT((slistelm), field);	\
SLIST_NEXT((slistelm), field) = (elm);				\
} while (0)

#define	SLIST_INSERT_HEAD(head, elm, field) do {			\
SLIST_NEXT((elm), field) = SLIST_FIRST((head));			\
SLIST_FIRST((head)) = (elm);					\
} while (0)

#define	SLIST_NEXT(elm, field)	((elm)->field.sle_next)






[ Post a follow-up to this message ]



    Re: Werid <sys/queue.h> - it has useless do{} while(0)  
Eric Sosman


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
04-27-07 12:17 AM

djhong@gmail.com wrote On 04/26/07 13:47,:
> (Since queue.h is not a part of standard C, I'd lke to ask here)
>
> For example, BSD implementation of sys/queue.h  has following weird
> macros:
> I can't understand why   "do{}while(0)"  is used...      Removing
> "do{}while(0)" from these macros should be ok. Right?
>
>
> #define	SLIST_INIT(head) do {						\
> 	SLIST_FIRST((head)) = NULL;					\
> } while (0)
>
> #define	SLIST_INSERT_AFTER(slistelm, elm, field) do {			\
> 	SLIST_NEXT((elm), field) = SLIST_NEXT((slistelm), field);	\
> 	SLIST_NEXT((slistelm), field) = (elm);				\
> } while (0)
>
> #define	SLIST_INSERT_HEAD(head, elm, field) do {			\
> 	SLIST_NEXT((elm), field) = SLIST_FIRST((head));			\
> 	SLIST_FIRST((head)) = (elm);					\
> } while (0)
>
> #define	SLIST_NEXT(elm, field)	((elm)->field.sle_next)

(You seem to be missing some backslashes.)

No reason I can see for do{}while(0) on the first
one, but for the next two it makes sense.  Maybe the
first has do{}while(0) just for consistency's sake, or
maybe an earlier version of the first macro had more
than one statement.

The reason for the do{}while(0) dodge in multi-
statement macro expansions is to make them work right
in contexts where just one statement is allowed.  For
example, try this simple macro:

#define MESSAGE(msg) { \
fputs (msg, stdout);
fputs (msg, stderr);
}

Easy enough, right?  MESSAGE("Hello, world!\n") writes
the message to two output streams.  But let's try it
in this context:

if (firstGreetingOfTheDay)
MESSAGE("Hello, world!\n");
else
MESSAGE("Hi again.\n");

(If you don't see the problem, feed it to a compiler
and see what it says.)

Now, you could "fix" this with documentation: "When
you use the MESSAGE macro, never put a semicolon at
the end."  But that's not a really wonderful solution
because it requires the programmer to be aware that this
"function-like macro" is really a macro and not an actual
function, and requires that it be coded with a different
style than pretty much everything else.  (And it will
probably raise merry Hell with automatic code formatters,
too.)  The do{}while(0) dodge turns the macro expansion
into a single semicolon-terminated statement, and the
programmer's life gets simpler.

--
Eric.Sosman@sun.com





[ Post a follow-up to this message ]



    Re: Werid <sys/queue.h> - it has useless do{} while(0)  
Eric Sosman


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
04-27-07 12:17 AM

Eric Sosman wrote On 04/26/07 14:34,:
> djhong@gmail.com wrote On 04/26/07 13:47,: 
> [...]
>
>     (You seem to be missing some backslashes.)

Woops!  You're not missing backslashes; I was
using too narrow a window.  Sorry about that.

--
Eric.Sosman@sun.com





[ Post a follow-up to this message ]



    Re: Werid <sys/queue.h> - it has useless do{} while(0)  
karl malbrain


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
04-27-07 12:17 AM

"Eric Sosman" <Eric.Sosman@sun.com> wrote in message
news:1177612444.288279@news1nwk...
> djhong@gmail.com wrote On 04/26/07 13:47,: 
>
>     The reason for the do{}while(0) dodge in multi-
> statement macro expansions is to make them work right
> in contexts where just one statement is allowed.

At the expense of contexts where no statements are allowed -- only
expressions.

karl m







[ Post a follow-up to this message ]



    Re: Werid <sys/queue.h> - it has useless do{} while(0)  
Eric Sosman


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
04-27-07 12:17 AM

karl malbrain wrote On 04/26/07 15:19,:
> "Eric Sosman" <Eric.Sosman@sun.com> wrote in message
> news:1177612444.288279@news1nwk...
> 
>
>
> At the expense of contexts where no statements are allowed -- only
> expressions.

Right.  But since there are no expressions that can
contain statements anyhow, much less the expansion of a
multi-statement macro, the expense is zero.

--
Eric.Sosman@sun.com





[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 06:22 PM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register