Saturday, January 21, 2006

Is a[i] = i++; valid?

This is a very basic question, but many intersting concepts emerge out of it.

Answer is "Behaviour is Undefined".A compiler may do anything it likes when faced with undefined behavior (and, within limits, with implementation-defined and unspecified behavior), including doing what you expect.

Briefly: implementation-defined means that an implementation must choose some behavior and document it. Unspecified means that an implementation should choose some behavior, but need not document it. Undefined means that absolutely anything might happen. In no case does the Standard impose requirements; in the first two cases it occasionally suggests (and may require a choice from among) a small set of likely behaviors.

The concept of sequence point can be used to base a discussion on
A sequence point is a point in time (at the end of the evaluation of a full expression, or at the ||, &&, ?:, or comma operators, or just before a function call) at which the dust has settled and all side effects are guaranteed to be complete.The ANSI/ISO C Standard states that
"Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be accessed only to determine the value to be stored."

The second sentence can be difficult to understand. It says that if an object is written to within a full expression, any and all accesses to it within the same expression must be for the purposes of computing the value to be written. This rule effectively constrains legal expressions to those in which the accesses demonstrably precede the modification(as above).

4 comments:

Srinivas said...

Many people make this kind of mistake. The other very popular example of this kind would be:

i = 10;
printf("%d %d \n", i++, ++i);

This is more related to the unspecified order in which arguments to a function may be evaluated.

Arvind said...
This comment has been removed by a blog administrator.
Arvind said...

I really didnt get it. Can you please explain Sequence point w.r.t to the example you have given. a[i]=i++;

Shantanu said...

http://en.wikipedia.org/wiki/Sequence_point
Is a well articulated document.

A sequence point is one where the output at the end of it is all ways the same.

Now here in a[i]=i++
say i's value is in a register then during the course of the execution the value of i is getting changed in the register.
Now here is when the execution becomes unpredictable