Thread: do {} while (0); ??

  1. #1
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709

    do {} while (0); ??

    I just found this (browsing Python source code):

    Code:
    #define Py_CLEAR(op)				\
            do {                            	\
                    if (op) {			\
                            PyObject *tmp = (PyObject *)(op);	\
                            (op) = NULL;		\
                            Py_DECREF(tmp);		\
                    }				\
            } while (0)
    I'm interested in the do..while block. Why did he / she / it do that ? I vaguely remember it being mentioned in one of my C++ books that I haven't read for ages, but can't remember for the life of me what the point of it is.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  2. #2
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    There is no point to it!!
    You can comment the loop out!!

  3. #3
    Registered User
    Join Date
    Oct 2004
    Posts
    151
    It's so you can put a semicolon after the macro when you use it, as if it were a real function.

    esbo, seriously, stop posting.
    System: Debian Sid and FreeBSD 7.0. Both with GCC 4.3.

    Useful resources:
    comp.lang.c FAQ | C++ FQA Lite

  4. #4
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    But I can put a semicolon after a macro when I use it already. Or is this just the "goody" way to do it?
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  5. #5
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    You don't want to leave an if statement on the outside of a macro, because it's essentially hidden to the coder. Consider if you did this:
    Code:
    if(strcmp(myVar,"Clear")==0)
       Py_CLEAR(toBeCleared)
    else
       fprintf(stdout, "Not clearing.");
    A superficial example with little to no difference either way, but regardless. Without the loop, there would be confusion as to what if that else belongs to. Of course, if you know the macro, you know what's happening. If always a good habit (as if macros were ever a good habit in the first place) to wrap if statements in a solid control structure in a macro, if you don't have an else.
    Sent from my iPadŽ

  6. #6
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by zx-1
    It's so you can put a semicolon after the macro when you use it, as if it were a real function.

    esbo, seriously, stop posting.
    If you don't like my posts don't read them!!
    I don't like your's so your on iggy.
    Goodbybe!!

  7. #7
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Quote Originally Posted by SlyMaelstrom
    You don't want to leave an if statement on the outside of a macro, because it's essentially hidden to the coder. Consider if you did this:
    Code:
    if(strcmp(myVar,"Clear")==0)
       Py_CLEAR(toBeCleared)
    else
       fprintf(stdout, "Not clearing.");
    A superficial example with little to no difference either way, but regardless. Without the loop, there would be confusion as to what if that else belongs to. Of course, if you know the macro, you know what's happening. If always a good habit (as if macros were ever a good habit in the first place) to wrap if statements in a solid control structure in a macro, if you don't have an else.
    Okies.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >If you don't like my posts don't read them!!
    I don't like your posts because they're always wrong but you try to speak with authority. That's misinformation in my opinion, and it goes against everything we try to do here. Since my mission is to provide accurate information, I don't have the option of not reading your posts. Therefore, you would do well to learn what people try to teach you and apply that knowledge in the future. If not then we'll have no choice but to label you as a troll and deal with you accordingly.

    >But I can put a semicolon after a macro when I use it already.
    But does the compiler throw an error if you fail to do so? The while loop is a clever way to enforce common C syntax with macros.
    My best code is written with the delete key.

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Quote Originally Posted by esbo
    If you don't like my posts don't read them!!
    I don't like your's so your on iggy.
    Goodbybe!!
    The problem with yours is that they're wrong! And how are beginners supposed to know that? Do you have a big warning in your signature saying, "Don't read my posts, they're most likely wrong!" ? You don't, and that's why you'd better refrain from posting.


    On topic, another reason is that the do...while forces the semicolon. I think that's what zx-1 meant. It makes the macro "statement-like" instead of "expression-like".

    Edit: Beaten on both points by Prelude ...
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    It's basically to make the macro syntactically a single statement
    http://c-faq.com/cpp/multistmt.html
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    -AppearingOnThis..........
    Join Date
    May 2005
    Location
    Netherlands
    Posts
    44
    This may seem like weird thing to do, but I personally like to use do {} while(0); loops when I have a decently sized procedure where an error could be encountered in several places, so I can easily break out of the sequence without using goto statements. It also enables me to save cleaning up possibly allocated resources until the end.

  12. #12
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >so I can easily break out of the sequence without using goto statements
    Oddly enough, I see that as a worse solution than goto. "Use judiciously" doesn't mean "avoid at all costs even when the result sucks".
    My best code is written with the delete key.

  13. #13
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Personally I avoid using macros unless I know what they do.

  14. #14
    -AppearingOnThis..........
    Join Date
    May 2005
    Location
    Netherlands
    Posts
    44
    I didn't mean that I religiously try to avoid using gotos, I do use them where I need to. To tell the truth, I don't really know what got me on to doing that, the general idea is the same, I guess I just sort of figured I might as well use a loop when I can...
    Last edited by SirNot; 09-11-2006 at 01:58 PM.

  15. #15
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Quote Originally Posted by Prelude
    >If you don't like my posts don't read them!!
    I don't like your posts because they're always wrong but you try to speak with authority. That's misinformation in my opinion, and it goes against everything we try to do here. Since my mission is to provide accurate information, I don't have the option of not reading your posts. Therefore, you would do well to learn what people try to teach you and apply that knowledge in the future. If not then we'll have no choice but to label you as a troll and deal with you accordingly.

    >But I can put a semicolon after a macro when I use it already.
    But does the compiler throw an error if you fail to do so? The while loop is a clever way to enforce common C syntax with macros.
    I have had the occasional complaint when using some macros inside if blocks. Complaints from my compiler that is.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

Popular pages Recent additions subscribe to a feed