8

I'm using open source project (NSBKeyframeAnimation) for some of animations in my p roject. Here are example of methods that i'm using:

double NSBKeyframeAnimationFunctionEaseInQuad(double t,double b, double c, double d)
{
    return c*(t/=d)*t + b;
}

I have updated my Xcode to 5.0, and every method from this project started to show me warnings like this: "Unsequenced modification and access to 't' ". Should i rewrite all methods to objective-c or there's another approach to get rid of all these warnings?

Lucas Eduardo
  • 11,525
  • 5
  • 44
  • 49
Vlad Z.
  • 3,401
  • 3
  • 32
  • 60

3 Answers3

16

The behavior of the expression c*(t/=d)*t + b is undefined, and you should fix it, e.g. to

t /= d;
return c*t*t + b;

See for example Undefined behavior and sequence points for a detailed explanation.

Community
  • 1
  • 1
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
5

those warnings can be disabled

put this before the code triggering the warning

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunsequenced"

and this after that code

#pragma clang diagnostic pop

however, nothing guarantees that compilers will always handle this case gracefully. i came to this page because i got 50 of those warnings, in exactly the same source file.

i'm grateful for those functions, but the programmer should realize that trying to write everything on one line is very "1980's", when the compilers weren't nearly as optimized as today.

and when it actually matter to win a few processor cycles, we only had a few million, not the billions we have now.

i would always put readability first.

Pizzaiola Gorgonzola
  • 2,789
  • 1
  • 17
  • 12
  • Minor point: Writing everything on one line is NOT more efficient. Even in the 80s it didn't make much difference. – Hot Licks Oct 07 '13 at 16:06
  • 1
    in the eighties writing a++ or a = a + 1 wasn't generating the same code. – Pizzaiola Gorgonzola Oct 24 '13 at 19:41
  • But there were some cases where `a++` was actually slower. – Hot Licks Oct 24 '13 at 20:42
  • 2
    Sometimes, you need one line code because the code is in a macro (using __LINE__), and there is no other solution for this. Sometimes you need one line because it's semantically short so you can focus on the actual operation, not the text itself. That's why people use x++ instead of x = x+1 which consumes more "brain time" to parse. – xryl669 Nov 08 '13 at 14:09
  • And - which is the main reason for those one liners - most of the time one liners are just improving readability a lot compared to multi liners – Kaiserludi Mar 11 '14 at 14:27
  • Speaking of optimization: postfix operators should not be used unless you really need the temporary value which is created. So instead of x++ rather go for ++x. It does make a difference. – Karoly Nyisztor Mar 24 '14 at 22:21
0

The error you are referring to appears in all versions of Xcode, seeing as it is not Xcode that is the source of the warning, but the programmer; and, it is not Xcode that generates the warning, it is the GCC compiler it uses to debug your code that is responsible for identifying the potential issue the warning raises.

While it may be that rewriting the expression will resolve the error, you do not have to do that in this case (or, technically, any other case like this). You can leave it as is.

The answer is to add sequence (or order) to the modification of the variable (i.e., to the assignment of a new value) and to its expression (i.e., to the return of its new value), and that only takes a few extra characters to achieve, namely, a pair of braces ensconced in parentheses and a semicolon (see Statements and Declarations in Expressions).

double NSBKeyframeAnimationFunctionEaseInQuad(double t,double b, double c, double d)
{
    return c*({(t/=d);})*t + b;
}

Here are before-and-after screenshots that demonstrate the solution:

The warning generated by the expression returning the value of the function in question

The modified return expression that satisfies the basis for the warning

James Bush
  • 1,485
  • 14
  • 19