line 453
while (0);
redundant code?
Crafty 23.1: search.c
Moderators: hgm, Rebel, chrisw
-
- Posts: 198
- Joined: Thu Mar 09, 2006 2:44 am
- Location: Helsinki, Finland
-
- Posts: 20943
- Joined: Mon Feb 27, 2006 7:30 pm
- Location: Birmingham, AL
Re: Crafty 23.1: search.c
Nope. Goes with the "do { }" where the "}" is right before the while(0);jarkkop wrote:line 453
while (0);
redundant code?
This makes the code easier to read, in that I want to get to the bottom of the loop without using a goto xxx where it is hard to figure out where xxx is. A "break" or "continue" will get you to that point.
-
- Posts: 838
- Joined: Thu Jul 05, 2007 5:03 pm
- Location: British Columbia, Canada
Re: Crafty 23.1: search.c
Yuck. Using a loop that doesn't loop, merely so you have somewhere to break to? Just to avoid one of the safest and least obnoxious uses of goto.bob wrote:Nope. Goes with the "do { }" where the "}" is right before the while(0);jarkkop wrote:line 453
while (0);
redundant code?
This makes the code easier to read, in that I want to get to the bottom of the loop without using a goto xxx where it is hard to figure out where xxx is. A "break" or "continue" will get you to that point.
This kind of trick doesn't work when other loops are involved (since C has no multi-level break). Of course its up to your personal coding style, but personally I would not write "do { } while(0)" anywhere but in a macro.
-
- Posts: 198
- Joined: Thu Mar 09, 2006 2:44 am
- Location: Helsinki, Finland
Re: Crafty 23.1: search.c
please check again from code because I couldn't a pair for do.
There is one
do
while(0);
but this seems extra.
There is one
do
while(0);
but this seems extra.
-
- Posts: 4052
- Joined: Thu May 15, 2008 9:57 pm
- Location: Berlin, Germany
- Full name: Sven Schüle
Re: Crafty 23.1: search.c
It *is* extra. The closing bracket in line 452 belongs to the while loop starting in line 246. The mentioned "do ... while(0);" is within that loop, from lines 258 to 401. So the "while(0);" in line 453 is indeed empty code.jarkkop wrote:please check again from code because I couldn't a pair for do.
There is one
do
while(0);
but this seems extra.
Of course, not a bug. Just redundant. The compiler removes it.
Sven
-
- Posts: 4052
- Joined: Thu May 15, 2008 9:57 pm
- Location: Berlin, Germany
- Full name: Sven Schüle
Re: Crafty 23.1: search.c
I can only second that. However my major point is that in a "do { } while(0)" construct, even more if it spans many lines, the use of "break" and "continue" makes the whole code difficult to read because both keywords have the same effect in this special case although you think that "continue" does something different in the first moment.wgarvin wrote:Yuck. Using a loop that doesn't loop, merely so you have somewhere to break to? Just to avoid one of the safest and least obnoxious uses of goto.bob wrote:This makes the code easier to read, in that I want to get to the bottom of the loop without using a goto xxx where it is hard to figure out where xxx is. A "break" or "continue" will get you to that point.
This kind of trick doesn't work when other loops are involved (since C has no multi-level break). Of course its up to your personal coding style, but personally I would not write "do { } while(0)" anywhere but in a macro.
Code that needs "goto" or such a "do ... while(0)" construct should be completely restructured IMO. I would use some inline functions to make the whole search function considerably smaller.
But of course, this is a matter of coding style, and the code seems to be correct.
Sven
-
- Posts: 20943
- Joined: Mon Feb 27, 2006 7:30 pm
- Location: Birmingham, AL
Re: Crafty 23.1: search.c
I've found it makes the code much easier to read. Search is quite long. A "goto xyz" could go anywhere in a thousand lines of code. A break simply jumps to the end of the do { } while(0); and since I use indent to maintain indentation levels, it is really easy to see where this is going. All a matter of taste, of course.wgarvin wrote:Yuck. Using a loop that doesn't loop, merely so you have somewhere to break to? Just to avoid one of the safest and least obnoxious uses of goto.bob wrote:Nope. Goes with the "do { }" where the "}" is right before the while(0);jarkkop wrote:line 453
while (0);
redundant code?
This makes the code easier to read, in that I want to get to the bottom of the loop without using a goto xxx where it is hard to figure out where xxx is. A "break" or "continue" will get you to that point.
This kind of trick doesn't work when other loops are involved (since C has no multi-level break). Of course its up to your personal coding style, but personally I would not write "do { } while(0)" anywhere but in a macro.
-
- Posts: 20943
- Joined: Mon Feb 27, 2006 7:30 pm
- Location: Birmingham, AL
Re: Crafty 23.1: search.c
we changed this from a do to a while() loop sometime back, and that extra while(0) was not noticed since the compiler removes it. I've fixed it. Thanks..jarkkop wrote:please check again from code because I couldn't a pair for do.
There is one
do
while(0);
but this seems extra.
Last edited by bob on Mon Nov 30, 2009 1:52 am, edited 1 time in total.
-
- Posts: 20943
- Joined: Mon Feb 27, 2006 7:30 pm
- Location: Birmingham, AL
Re: Crafty 23.1: search.c
You are correct. At some point this was changed from a do to a while at the top of the loop, and that superfluous while (0); was not removed since the compiler dumps it. I've removed it...Sven Schüle wrote:It *is* extra. The closing bracket in line 452 belongs to the while loop starting in line 246. The mentioned "do ... while(0);" is within that loop, from lines 258 to 401. So the "while(0);" in line 453 is indeed empty code.jarkkop wrote:please check again from code because I couldn't a pair for do.
There is one
do
while(0);
but this seems extra.
Of course, not a bug. Just redundant. The compiler removes it.
Sven
thanks