Linux Help
guides forums blogs
Home Desktops Distributions ISO Images Logos Newbies Reviews Software Support & Resources Linuxhelp Wiki

Welcome Guest ( Log In | Register )



Advanced DNS Management
New ZoneEdit. New Managment.

FREE DNS Is Back

Sign Up Now
 
Reply to this topicStart new topic
> Usage Of Do{ }while(0)
vasanth
post Mar 8 2005, 04:18 AM
Post #1


Whats this Lie-nix Thing?
*

Group: Members
Posts: 6
Joined: 26-February 05
Member No.: 4,701



Hello,
In the linux source code,we can see code which are enclosed by do{ statements; }while(0) .what is the use of this construct? will it not work without this do ..while construct.
Go to the top of the page
 
+Quote Post
Hemant
post Mar 11 2005, 08:12 AM
Post #2


RMS is my Hero
******

Group: Support Specialist
Posts: 782
Joined: 6-October 02
From: Trichy,INDIA
Member No.: 29



I dont know under what circustances this has been used,but certainly this has been used to execute something once.

Inside the loop some magic is done,which you missed probably.


--------------------
Hemant Kumar
user posted image
hemant@linuxhelp.net
Go to the top of the page
 
+Quote Post
vasanth
post Mar 12 2005, 05:33 AM
Post #3


Whats this Lie-nix Thing?
*

Group: Members
Posts: 6
Joined: 26-February 05
Member No.: 4,701



HI,
iam including the code from the kernel where linux circular linked lists are initialized(include/linux/list.h)

#define INIT_LIST_HEAD(ptr) do {
(ptr)->next = (ptr); (ptr)->prev = (ptr);
} while (0)

why is do { }while(0) used here? what purpose does it serve?
Go to the top of the page
 
+Quote Post
DS2K3
post Mar 12 2005, 09:43 AM
Post #4


Its GNU/Linuxhelp.net
*******

Group: Support Specialist
Posts: 1,284
Joined: 14-November 04
From: Southampton, Hampshire
Member No.: 4,165



do{}while is like suing while(), except that the statement in the while() brackets is only evaluated AFTER the first loop. I'm not a C programmer, but I tihnk 0 would make the evaluation false, and therefore the loop would only go round once.

D


--------------------
Fixed your problem? Let us know!
richard@linuxhelp.net

www.Gathr.co.uk Online Event Management
Go to the top of the page
 
+Quote Post
Hemant
post Mar 12 2005, 10:37 AM
Post #5


RMS is my Hero
******

Group: Support Specialist
Posts: 782
Joined: 6-October 02
From: Trichy,INDIA
Member No.: 29



Ok..i think i got it.

The code snippet you have given is clearly used to initialise a doubly linked list.Now the whole thing is defined as a macro.

But as you know macros are quite tricky and nasty stuff...and that is the reason why.
CODE
#define MUL(a,B) a*b

and

#define MUL(a,B) (a)*(B)


can give quite different results,depending upon the arguments passed to it.If you have got your lesson in C,you can easily figure out how?

So sometimes it becomes quite necessary to have parenthesis around your statements.Now in our case the whole macro is a bit complex and thus just as a workaround and to make sure that statements stay together for any kind of argument,they have used do{}while().

Otherwise for normal arguments,the do{}while() is redundant.It is just like making the code bulletproof.If you remove the do{}while() then you will have two statements for a single macro and it is quite obvious that only one will be used.of course they could have done something like this..
CODE
#define INIT_LIST_HEADER(ptr) {
(ptr)->next = (ptr); (ptr)->prev=(ptr);
}


But i think having workarounds is a matter of choice ,the guy who has written that preferred the earlier method.And may be it is more bulletproof.


--------------------
Hemant Kumar
user posted image
hemant@linuxhelp.net
Go to the top of the page
 
+Quote Post
imransadat
post Mar 30 2005, 10:20 AM
Post #6


Whats this Lie-nix Thing?
*

Group: Members
Posts: 1
Joined: 30-March 05
Member No.: 4,805



The do { } while(0) enclosure helps in making the group of statements in the macro into a single statement. e.g.

#define FOO(x) { function1(x); function(2); }
.
.
.

if (cond)
FOO(1);
else
FOO(2);

it will fail cause of the extra semi colon at the end. Also the do { } while(0) statement gives the macro the additional break statement which otherwise would need complex if else to implement.

try this page for more help http://c2.com/cgi/wiki?TrivialDoWhileLoop

Imran Sadat.
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 



RSS Lo-Fi Version Time is now: 14th December 2017 - 01:43 PM