Technical article 100791

Why doesn't the code: a[i]=i++; work?

OpenServer 5.0.0

Written · Last revised

Problem

Why will the following code not work?
a[i] = i++;

Cause

The subexpression i++ causes a side effect:  it modifies
i's value which leads to undefined behavior since i is also
referred to elsewhere in the same expression.

Solution

A more complete code stub which illustrates this issue is:

#include <stdio.h>
main()
{
   char a[] = "Some literal string";
   int i = 0;
   printf("a=%s\n",a);
   while (i < sizeof(a)) {
      printf("%s\n",a[i]);
      a[i] = i++;
   }
}

Because the behavior is undefined by C-language syntax rules, (see below), there is no way of knowing whether the reference will happen before or after the side effect.

Once an expression or a program becomes undefined, all aspects of the expression or the program become undefined.

For example, this expression is one that can have a different
value at different optimization levels even with the same
compiler.  When an undefined expressions has two possible
interpretations, do not assume that the compiler will choose
one interpretation or the other.

The standard does not require that a compiler make an obvious choice and some compilers do not. In the above example, we do not know if a[i] or a[i+1] is written to; it is possible that a completely unrelated part of the array, or any part of memory, is written to, and it is also not possible to predict what the final value of i will be.

Consider the following code stub:
int i = 7;
printf("%d\n",i++ * i++);
Regardless of the order of evaluation, shouldn't it print
the value 49?  (7*7)  Under OSR5's USL and icc compilers,
the value displayed is actually 56, (7*8) with the terminating
value for i=9.
What's true is that the postincrement and postdecrement
operators (++,--) perform their operations after returning
the former value.  What's not clear is when "after" actually
occurs.  An increment, or decrement, operator is not
guaranteed to be performed immediately after giving up the
previous value _and_ before any other part of the expression
is evaluated.  The only guarantee is that the update will be
performed sometime before the expression is considered
finished (before the next sequence point).  On a customer's
system where the value 49 was returned, the compiler chose
to multiply the previous value by itself and to perform both
increments afterward.
Code that contains multiple, ambiguous side effects is
undefined.  A single expression should not cause the same
object to be modified twice, or to be modified and then
inspected.

See also

TA100792, "What is implementation-defined, unspecified, and
undefined behavior?"
K&R1 S2.12, p.50
K&R2 S2.12, pp.53-54
ANSI S5.3
ISO  S6.3
NOTE:     lint(CP) will detect most dependencies on order of
          evaluation.

From the Xinuos technical article database, record 100791. Reproduced with permission. The original is at www.sco.com/ta/100791, which is reachable but blocked from search engines. Xinuos marks this article against a retired product line. That refers to the product’s lifecycle, not to whether the advice still works — plenty of it still applies to running systems.

Next step

Need this fixed rather than explained?

Tell us the release, what the system runs and what it is doing. We will reply by email with what we would do next.

  • You deal with the people who do the work
  • No obligation, and no upgrade required before we will help
  • We tell you plainly if something is not worth doing

Send an enquiry

We reply by email. If a server is down, say so in the first line. You can also write to help@scoexpert.com.