1

In C++ is there any difference in the assembly code generated by a statement such as:

if (*expr*) { }

vs.

if (*expr*) return;  

Basically what I want to know is whether or not delimiting an if statement with brackets makes any difference to the underlying code generated than a simple return statement, as above.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
Slothrop
  • 142
  • 7

6 Answers6

2

If there's going to be only one statement within the block then both are identical.

if (e)
{
   stmt;
}

if (e) stmt;

are the same. However, when you've more than one statement to be executed, it's mandatory to wrap them in {}.

legends2k
  • 31,634
  • 25
  • 118
  • 222
1

No, there is no difference between the two examples. You can use a profiler to see what assembly code is outputted and see for your self.

Kieren Pearson
  • 416
  • 3
  • 15
  • That your particular compiler happens to be emit the same asm for this test, doesn't mean they are the same. (Even if they are, like in this question) – erenon Jul 08 '15 at 07:02
0

Aside from the return statement, there is no difference.

A compiler may optimise out the first case altogether if the expression is either compile-time evaluable (e.g. sizeof) or has no side-effects.

Similarly, the second case might be optimised out to a simple return;

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
0

This function …

void f1(int e) {
    if (e) {}
}

… compiles to …

f1:
    rep ret

… while this function …

void f2(int e) {
    if (e) return;
}

… compiles to …

f2:
    rep ret

… when optimization is enabled using the -O2 option.

dlask
  • 8,776
  • 1
  • 26
  • 30
0

If there is only one statement, then there is no difference in using "block" and "writing inline".

Braces are used only to enclose a sequence of statements that are intended to be seen as a single process.

General syntax for if:

if ( condition_in_the_diamond )
    statement_to_execute_if_condition_is_true;

So to make multiple lines of code as a single process, we use braces.

Hence if you have only one statement to execute in if statement, the it would be similar.


Using braces are better because it reduces the chances of error. Suppose you are commenting a line of code in hurry to debug:

if(condition)
//    statement1;
statement2;        //this will bring statement2 in if clause which was not intended

or while adding a line of code:

if(condition)
    statement1;
    statement3;    // won't be included in if
statement2;

But if you are using inline statement as:

if(condition) statement1;

then it might prevent you from above error but it will make statement1 of limited length (assuming 80 character width code). That will make it sweet and simple to read though.

Hence unless you are using inline statement, using braces is suggested.

Source

Community
  • 1
  • 1
Rakholiya Jenish
  • 3,165
  • 18
  • 28
0

With poorly-written macros nasty things can happen.

// c-style antipattern. Better use scoped_ptr
#define CLEANUPANDRETURN delete p; return

if (*expr*) CLEANUPANDRETURN;

// preprocesses to: (with added linebreaks)
if (*expr*)
  delete p;
return;

Many functions of the C-Library may actually be macros. See Why use apparently meaningless do-while and if-else statements in macros? for a trick of somehow safer macros.

Community
  • 1
  • 1
Markus Kull
  • 1,471
  • 13
  • 16
  • The writers of the C library implementations have figured out how to avoid such nasties; they typically will use `do { ... } while(false)` inside their function-like macro's. – MSalters Jul 08 '15 at 10:41