Click here to Skip to main content
15,920,217 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,
I'm studying select in network programming. I have a question when encountering the implementation of FD_SET in Windows(as follow). My question is why redundantly use do-while ?
Thanks for your attention.
C++
#define FD_SET (fd, set) do { \
    u_int __i; \
    for ( __i = 0; __i < ((fd_set FAR *)(set ))->fd_count; __i++) { \
        if (((fd_set FAR *)( set))->fd_array [__i] == ( fd)) { \
            break; \
        } \
    } \
    if ( __i == ((fd_set FAR *)( set))->fd_count ) { \
        if (((fd_set FAR *)( set))->fd_count < FD_SETSIZE) { \
            (( fd_set FAR *)(set))-> fd_array[__i ] = (fd); \
            (( fd_set FAR *)(set))-> fd_count++; \
        } \
    } \
} while(0)
Posted
Comments
Richard MacCutchan 26-Feb-15 4:13am    
It just forces the code to be run once. It has no practical use, except within a complex macro such as you have shown.

1 solution

 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 25-Feb-15 22:41pm    
"Nonsense" is a good word here, a 5.
—SA
cxxxxf 26-Feb-15 1:05am    
Sorry, My English is poor. Why "nonsense" is a good word and what is the meaning of "a 5".
Sergey Alexandrovich Kryukov 26-Feb-15 8:42am    
5 is my vote for this answer (means "excellent" on the 1 to 5 scale). I agree that using "while(0)" makes no sense ("nonsense") or ridiculously little sense, inadequate. This is what this word means. Not bad, I think.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900