LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-07-2013, 03:56 PM   #1
dbrazeau
Member
 
Registered: Aug 2009
Distribution: Fedora, OpenSuse, DENX Embedded Linux
Posts: 184

Rep: Reputation: 28
warning: operation on 'i' may be undefined [-Wsequence-point]


So here is an example of the code it is complaining about.
Code:
unsigned i;
i = ++i % 10;
Is this really an invalid expression?

Obviously I could just "fix" it by doing something like this.
Code:
unsigned i;
i++;
if(i >= 10)
    i = 0;
Edit:
This is in C/C++.

Last edited by dbrazeau; 11-07-2013 at 06:14 PM.
 
Old 11-07-2013, 04:33 PM   #2
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,790

Rep: Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653
It's likely complaining because you haven't initialised 'i' before using it.
 
Old 11-07-2013, 04:58 PM   #3
dbrazeau
Member
 
Registered: Aug 2009
Distribution: Fedora, OpenSuse, DENX Embedded Linux
Posts: 184

Original Poster
Rep: Reputation: 28
Quote:
Originally Posted by kbp View Post
It's likely complaining because you haven't initialised 'i' before using it.
My code is a little more complicated than that. It is more like:
Code:
void function(SomeType* ptr)
{
   ptr->count = ++ptr->count % 10;
   return;
}
In which case ptr->count has been initialized prior to calling this function.

Should also mention that despite the warning the code works fine.

Last edited by dbrazeau; 11-07-2013 at 05:15 PM.
 
Old 11-07-2013, 05:18 PM   #4
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,790

Rep: Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653
Ah .. there's a good description of the situation here - basically the order of operations is ambiguous.
 
Old 11-07-2013, 05:56 PM   #5
dbrazeau
Member
 
Registered: Aug 2009
Distribution: Fedora, OpenSuse, DENX Embedded Linux
Posts: 184

Original Poster
Rep: Reputation: 28
Quote:
Originally Posted by kbp View Post
Ah .. there's a good description of the situation here - basically the order of operations is ambiguous.
I actually came across this exact same article when researching this and it makes sense, but then I also came across this table (http://msdn.microsoft.com/en-us/libr...(v=vs.60).aspx) which indicates that Pre-increment has a higher precedence than %, so in that case it seems like order of operations shouldn't be ambiguous.

My only guess is that they actually have the same precedence, and according to this website http://www.c4learn.com/c-programming...dence-and.html they have opposite associativity which would explain why the compiler complains that order of operations is ambiguous.

Edit:
Well found a couple other sites (http://www.isthe.com/chongo/tech/com...recedence.html and http://www.swansontec.com/sopc.html) which indicate that increment(++) does actually have a higher precedence than %, so not sure how the order of operations can be ambiguous.

Last edited by dbrazeau; 11-07-2013 at 06:05 PM.
 
1 members found this post helpful.
Old 11-07-2013, 06:50 PM   #6
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,774

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
Quote:
Originally Posted by dbrazeau View Post
My only guess is that they actually have the same precedence, and according to this website http://www.c4learn.com/c-programming...dence-and.html they have opposite associativity which would explain why the compiler complains that order of operations is ambiguous.
That's not the problem. What the compiler is complaining about is that you reference the variable ptr again (on the LHS) in the same statement where it is incremented. The ambiguity is whether the LHS will use the pre-increment or post-increment value.

Technically, the result is "undefined," which means that any result, including reformatting your disk, would be within the C language spec (though seriously in violation of the "principle of least surprise").


Sorry, had the operator precedence confused again.

Last edited by rknichols; 11-09-2013 at 01:38 PM.
 
Old 11-08-2013, 10:08 AM   #7
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
This is C FAQ 3.3, 3.8 for more detail.

Code:
i = (i + 1) % 10;
should work.
 
1 members found this post helpful.
Old 11-08-2013, 11:03 AM   #8
mina86
Member
 
Registered: Aug 2008
Distribution: Debian
Posts: 517

Rep: Reputation: 229Reputation: 229Reputation: 229
Quote:
Originally Posted by dbrazeau View Post
I actually came across this exact same article when researching this and it makes sense, but then I also came across this table (http://msdn.microsoft.com/en-us/libr...(v=vs.60).aspx) which indicates that Pre-increment has a higher precedence than %, so in that case it seems like order of operations shouldn't be ambiguous.
This is only relevant when parsing the source code. “++i % 10” is parsed as “(++i) % 10” (and not “++(i % 10)”. The compiler can perform operations in whatever order it pleases (and in whatever way it pleases) as long as the result after the C instruction is the same provided code is valid.

Say you have: “j = ++i % 10”. Compiler might translate it to:
Code:
1. Load variable i to register α.
2. Increment value in α.
3. Save reminder of dividing α by 10 to register β.
4. Save β to j.
5. Save α to i.
The compiler might also swap 4th and 5th operation and the same end result would be produced. Now, if you have “i = ++i % 10”, compiler is free to apply the same logic meaning that you don't know what which value will end up in i (since 4th and 5th operation might be in any order).

For more see already referenced C FAQ 3.8.

Quote:
Originally Posted by rknichols View Post
What the compiler is complaining about is that you reference the variable ptr again (on the LHS) in the same statement where it is incremented. The ambiguity is whether the LHS will use the pre-increment or post-increment value.
ptr is never modified, this is not the issue. You probably meant ptr->count.

Last edited by mina86; 11-08-2013 at 11:07 AM. Reason: fixing formatting
 
1 members found this post helpful.
Old 11-08-2013, 11:40 AM   #9
dbrazeau
Member
 
Registered: Aug 2009
Distribution: Fedora, OpenSuse, DENX Embedded Linux
Posts: 184

Original Poster
Rep: Reputation: 28
Thanks for the great responses.

Quote:
Originally Posted by mina86 View Post
This is only relevant when parsing the source code. “++i % 10” is parsed as “(++i) % 10” (and not “++(i % 10)”. The compiler can perform operations in whatever order it pleases (and in whatever way it pleases) as long as the result after the C instruction is the same provided code is valid.

Say you have: “j = ++i % 10”. Compiler might translate it to:
Code:
1. Load variable i to register α.
2. Increment value in α.
3. Save reminder of dividing α by 10 to register β.
4. Save β to j.
5. Save α to i.
The compiler might also swap 4th and 5th operation and the same end result would be produced. Now, if you have “i = ++i % 10”, compiler is free to apply the same logic meaning that you don't know what which value will end up in i (since 4th and 5th operation might be in any order).

For more see already referenced C FAQ 3.8.
Thanks that makes a lot of since.

I guess I had some incorrect assumptions like with the pre-increment it would perform step 5 after step 2, and definitely before step 4, and that post-increment, i.e. j = i++ % 10, would perform steps 2 and 5 last.

I think I will just use this method, which will eliminate step 5.
Quote:
Originally Posted by ntubski View Post
This is C FAQ 3.3, 3.8 for more detail.

Code:
i = (i + 1) % 10;
should work.

Last edited by dbrazeau; 11-08-2013 at 11:57 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Warning: GDB: Failed to set controlling terminal: Operation not permitted dschornack Programming 0 08-04-2009 07:14 PM
Different floating point operation results on AMD and Intel ykwok Linux - General 4 06-17-2008 06:56 AM
warning undefined functions.....doing kernel module programing swift_a2002 Programming 10 05-24-2007 11:50 AM
warning : /usr/lib64/libavcodec.so: undefined symbol: awt586 SUSE / openSUSE 2 07-19-2005 04:18 PM
How can i set access point address ? i getting operation not supported error sahil1 Linux - Hardware 0 03-14-2004 02:17 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:26 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration