Re: why do we use while (0)

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

 



The intent is just to enclose the code of the macro in a block, so that if you were to place this macro after an if() statement, and the macro was multi-line, it would not break things.

eg.

#define crazy(a, b)  \
a = b; \
b = a;

if(\*something*\)
   crazy(a,b)

Look at this carefully, which line do you think is associated with the if() here? Only a = b;, NOT b =a; !

but if the macro was written like this:

#define crazy(a, b)  \
do { \
a = b; \
b = a; \
}

both the lines would be associated with the "true" case of if() above.

HTH,
Mayank

On Dec 26, 2007 11:44 PM, Maneesh Singhal <mnshsnghl@xxxxxxxxx > wrote:
One more reason could be, by using do-while(0) thingy, one could use
'break' statement at any place just to come out of the macro at once.

Thanks.
Anupam Kapoor wrote:
> sahlot arvind <asahlot@gmail> wrote:
> ,----
> | > Recently I started looking into linux kernel and trying to understand
> | > the code.  I am working with linux-2.6.9.  in file
> | > include/llinux/list.h - I found something like this.
> | >
> | > #define INIT_LIST_HEAD(ptr) do { \
> | > (ptr)->next = (ptr); (ptr)->prev = (ptr); \
> | > } while (0)
> | >
> | >
> | > My question is why do we use a loop when we actually know that it is
> | > not going to execute more than once? Cannot we simply do -
> `----
> see: http://kernelnewbies.org/FAQ/DoWhile0
>
> anupam
>
> --
> To unsubscribe from this list: send an email with
> "unsubscribe kernelnewbies" to ecartis@xxxxxxxxxxxx
> Please read the FAQ at http://kernelnewbies.org/FAQ
>
>
>

--
To unsubscribe from this list: send an email with
"unsubscribe kernelnewbies" to ecartis@xxxxxxxxxxxx
Please read the FAQ at http://kernelnewbies.org/FAQ




--
KTHXBAI

-Mayank

[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux