do {…} while(false)

前端 未结 25 2297
小鲜肉
小鲜肉 2020-11-28 03:29

I was looking at some code by an individual and noticed he seems to have a pattern in his functions:

 function()
{
 

        
相关标签:
25条回答
  • 2020-11-28 03:46

    How old was the author?

    I ask because I once came across some real-time Fortran code that did that, back in the late 80's. It turns out that is a really good way to simulate threads on an OS that doesn't have them. You just put the entire program (your scheduler) in a loop, and call your "thread" routines" one by one. The thread routines themselves are loops that iterate until one of a number of conditions happen (often one being a certain amount of time has passed). It is "cooperative multitasking", in that it is up to the individual threads to give up the CPU every now and then so the others don't get starved. You can nest the looping subprogram calls to simulate thread priority bands.

    0 讨论(0)
  • 2020-11-28 03:48

    In addition to the already mentioned 'goto examples', the do ... while (0) idiom is sometimes used in a macro definition to provide for brackets in the definition and still have the compiler work with adding a semi colon to the end of a macro call.

    http://groups.google.com/group/comp.soft-sys.ace/browse_thread/thread/52f670f1292f30a4?tvc=2&q=while+(0)

    0 讨论(0)
  • 2020-11-28 03:48

    This is amusing. There are probably breaks inside the loop as others have said. I would have done it this way :

    while(true)
    {
       <main code for function>
       break; // at the end.
    }
    
    0 讨论(0)
  • 2020-11-28 03:49

    It is a very common practice. In C. I try to think of it as if you want to lie to yourself in a way "I'm not using a goto". Thinking about it, there would be nothing wrong with a goto used similarly. In fact it would also reduce indentation level.

    That said, though, I noticed, very often this do..while loops tend to grow. And then they get ifs and elses inside, rendering the code actually not very readable, let alone testable.

    Those do..while are normally intended to do a clean-up. By all means possible I would prefer to use RAII and return early from a short function. On the other hand, C doesn't provide you as much conveniences as C++ does, making a do..while one of the best approaches to do a cleanup.

    0 讨论(0)
  • 2020-11-28 03:50

    I've seen it used as a useful pattern when there are many potential exit points for the function, but the same cleanup code is always required regardless of how the function exits.

    It can make a tiresome if/else-if tree a lot easier to read, by just having to break whenever an exit point is reached, with the rest of the logic inline afterwards.

    This pattern is also useful in languages that don't have a goto statement. Perhaps that's where the original programmer learnt the pattern.

    0 讨论(0)
  • 2020-11-28 03:50

    The other reason I can think of is that it decorates the braces, whereas I believe in a newer C++ standard naked braces are not okay (ISO C doesn't like them). Otherwise to quiet a static analyzer like lint.

    Not sure why you'd want them, maybe variable scope, or advantage with a debugger.

    See Trivial Do While loop, and Braces are Good from C2.

    To clarify my terminology (which I believe follows standard usage):

    Naked braces:

    init();
    ...
    {
    c = NULL;
    mkwidget(&c);
    finishwidget(&c);
    }
    shutdown();
    

    Empty braces (NOP):

    {}
    

    e.g.

    while (1)
       {}  /* Do nothing, endless loop */
    

    Block:

    if (finished)
    {
         closewindows(&windows);
         freememory(&cache);
    }
    

    which would become

    if (finished)
         closewindows(&windows);
    freememory(&cache);
    

    if the braces are removed, thus altering the flow of execution, not just the scope of local variables. Thus not 'freestanding' or 'naked'.

    Naked braces or a block may be used to signify any section of code that might be a potential for an (inline) function that you wish to mark, but not refactor at that time.

    0 讨论(0)
提交回复
热议问题