Is this behavior undefined in C ++?

I was wondering if there is access to x in the latter, if below, the behavior is undefined or not:

int f(int *x) { *x = 1; return 1; } int x = 0; if (f(&x) && x == 1) { // something } 
+4
source share
7 answers

This behavior is not undefined as the && operator is a sequence point.

+13
source

It is clearly defined.

Link - C ++ 03 Standard:

Section 5: Expressions, Paragraph 4:

except as noted [for example, special rules for && & and || ], the procedure for evaluating the operands of individual operators and subexpressions of individual expressions, and the order in which side effects occur, Unspecified .

Until,

Section 1.9.18

When evaluating the following expressions

 a && b a || b a ? b : c a , b 

using the built-in value of the operators in these expressions, there is a point in the sequence after evaluating the first expression (12).

+7
source

It is determined. C / C ++ makes a lazy evaluation, and it is determined that the left expression will be evaluated and checked first. If this is true, then it will be correct.

+2
source

No, because && determines the order in which lhs should be calculated before rhs.

There is a certain order also on || , ?: and,. There are no other operands.

In comparable:

 int x = 0; if (f(&x) & x == 1) { // something } 

Then it is undefined. Here, both lhs and rhs will be calculated in any order. This unobtrusive form is logical and less common, as short cuts are usually seen as at least profitable for performance and often vital for correctness.

+2
source

This is not undefined behavior. The reason depends on two facts, both of which are sufficient to determine a specific behavior.

  • The function call and its end is the point of the sequence
  • "& &" operator - sequence point

The following is also indicated.

 int f(int *x) { *x = 1; return 1; } int x = 0; if (f(&x) & (x == 1)) { // something } 

However, you do not know if x == 1 matches true or false , because the first or second operand & can be evaluated first. However, this is not important for determining the behavior of this code.

+2
source

It is not undefined, but it should not compile either, as you are trying to assign a pointer to x ( &x ).

&& will be evaluated from left to right (evaluation will stop if the left side evaluates to false).

Edit: upon change, it should compile, but it will still be defined (since it doesn’t really matter if you use a pointer or a link).

+1
source

It will pass the address of the local variable x in the block of the caller as parameter f (pointer to int). f will then set the parameter (which is a temporary variable on the stack) to address 1 (this does not cause problems) and returns 1. Since 1 is true, if () will move on to evaluate x == 1, which is false, since x in the main block is still 0.

The body of the if block will not be executed.

EDIT

With your new version of the question, the body will be executed, because after returning f () x in the calling block there will be 1.

0
source

Source: https://habr.com/ru/post/1390851/


All Articles