Error in Optimized Code (pre-increment)

cancel
Showing results for 
Search instead for 
Did you mean: 

Error in Optimized Code (pre-increment)

2,171 Views
cws
Contributor I
I was debugging a problem and noticed some unexpected output with the following code:

void SomeFunct(void)
{
char buffer[100];
int j;

j = 0;

Foo( buffer[++j], j );
}

void Foo(char *buffer, int index)
{

}

With optimization at level 4, index in Foo(...) would be passed in as 0, with optimization off (level 0) index would be 1. I just moved the increment out of the call to Foo(…) to get things working but wanted to report this problem (especially since the source code that exposed the problem was from a Freescale seminar.)

I’m using CodeWarrior Development Studio for ColdFire Architectures Version 6.3 (Preview Release) and targeting the MCF52233 Coldfire.
Labels (1)
0 Kudos
4 Replies

327 Views
CrasyCat
Specialist III
Hello
 
I would recommend you to rewrite the code as follows:
  j++;
  Foo( buffer[j], j );
Article 6.3.2.2 in the ANSI standard specifies that
"The order of evaluation of the function designator , the arguments and subexpressions within the arguments is unspecified <...>".
 
So that means when you are writing
    Foo( buffer[++j], j );
the standard does not specify which parameter is evaluated first. Might be the first one or the second one.
 
When -O3 is activated the compiler is passing second parameter first and then the first one.
This is compliant to the standard and I am not sure we can call that a bug.
If you still feels that is a defect, please submit a service request  for that.
 
To log the issue with Freescale please go to following URL:
    http://www.freescale.com/webapp/sps/site/homepage.jsp?nodeId=054670&tid=FSH
and click on "Submit a service request"
 
CrasyCat
0 Kudos

327 Views
cws
Contributor I
I didn't realize the standard was vague on that point. However, I would expect optimized code to produce the same results as non-optimized code with the same compiler. Is this not a reasonable/reliable assumption?
0 Kudos

327 Views
CrasyCat
Specialist III
Hello
 
I would not take and assumption at that level. But as I was specifying in my last post, if you feel that this should be changed, you need to submit a service request  for that.
 
Per default, CodeWarrior for Coldfire compiles source code using the language specified by the ISO/IEC 9899-1990 (“C89”) standard.
So I was referring to that standard.
 
Note it is possible to switch to c99 standard, but I do not think the standard has changed around  the order of evaluation of the function arguments.
 
CrasyCat
0 Kudos

327 Views
Lundin
Senior Contributor IV
Sorry for getting off-topic, but which version of ANSI C is that? I'm checking "C99" (ISO/IEC9899:1999) and find that line on 6.5.2.2.10.
0 Kudos