Static code analysis

Programming, for all ages and all languages.
User avatar
qw
Member
Member
Posts: 792
Joined: Mon Jan 26, 2009 2:48 am

Re: Static code analysis

Post by qw »

Not an another argument, please? There is another solution, I'm giving it. That's all.
Last edited by qw on Mon Mar 26, 2012 12:58 am, edited 1 time in total.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: Static code analysis

Post by Solar »

No, not an argument. Sorry if I didn't make that clear - there has to be some difference involved with this syntax, some advantage or somesuch (or the GCC people wouldn't have bothered with it), and I'd like to know about it.
Every good solution is obvious once you've found it.
User avatar
qw
Member
Member
Posts: 792
Joined: Mon Jan 26, 2009 2:48 am

Re: Static code analysis

Post by qw »

Well, personally, I never liked the "do { } while (0)" because it was not intended to be used that way. It does not show what it is supposed to do, and it confuses people who are unfamiliar with the C preprocessor, even if they are experienced in other structured programming languages. And it can't return a value. I think the GCC solution is much more elegant.

After second thought, an inline function is probably the most elegant solution, and standard conforming too.
Snake
Posts: 19
Joined: Wed Mar 21, 2012 7:38 pm

Re: Static code analysis

Post by Snake »

Solar wrote:No, not an argument. Sorry if I didn't make that clear - there has to be some difference involved with this syntax, some advantage or somesuch (or the GCC people wouldn't have bothered with it), and I'd like to know about it.
This gcc extension is which makes it possible to treat a statement as an expression. Which is an additional functionality not available in the

Code: Select all

do { ... } while(0)
You can do things like:

Code: Select all

  ({ int y = foo (); int z;
        if (y > 0) z = y;
        else z = - y;
        z; })
Which will evaluate to the absolute value of foo. In contrast the do/while loop does not evaluate to anything.

More info here: http://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html
Gigasoft
Member
Member
Posts: 855
Joined: Sat Nov 21, 2009 5:11 pm

Re: Static code analysis

Post by Gigasoft »

I got a false V595 in PVS Studio from the following:

Code: Select all

if(SomeFunction(p)) {
    SomeOtherFunction(p->data);
}
if(p) {
Post Reply