Re: OSS sound

"Lev I." <[email protected]>
Newsgroups gmane.comp.emulators.winex.devel
Message-ID <[email protected]>
Rob Crittenden wrote:

>Yes, I understand how macros work. My question is why have a do/while loop that
>will always exit at the first test? Why not write the macros as:
>
>...
>#define CLEAR_OMR(omr) { int x = 0; read((omr)->msg_pipe[0], &x, sizeof(x)); }
>#define RESET_OMR(omr) { } 
>...
>
>It just seems like a strange thing to do.
>  
>
Rob,

When you're writing a macro with braces {}, you'll need to swallow the 
semicolon,
as Peter pointed out.
Suppose:

#define MACRO(x)  { x = 5; }

now I do :

if (abc)
    MACRO(abc);
else
    something else;

This exapands to :

if (abc)
    { abc = 5; };
else
    something else;

This will give a compilation error, since you now have an extra 
semicolon after
the braces, which makes it 2 statements after the if. The else does not 
match up
to the if!
To avoid this problem, whenever a macro contains multiple statements, it is
enclosed in a do {} while(0). (Note: without the trailing semicolon!)
This way the macro expands to :

if (abc)
    do { abc = 5; } while(0);
else
    something else;

This allows the macro to contain multiple statements in a block of code, 
while
still looking like one statement to the user.

(BTW, this does not induce any performance penalty, since the 
always-false check
is optimized out by the compiler).

--Lev



-------------------------------------------------------
This SF.net email is sponsored by: SF.net Giveback Program.
Does SourceForge.net help you be more productive?  Does it
help you create better code?   SHARE THE LOVE, and help us help
YOU!  Click Here: http://sourceforge.net/donate/
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.