PRB: C 6.0a(x) Code Incorrect for Array Index in Loop with /Ol

ID Number: Q70261

6.00a 6.00ax | 6.00a

MS-DOS | OS/2

buglist6.00a buglist6.00ax fixlist7.00

Summary:

SYMPTOMS

The Microsoft C Compiler versions 6.0a and 6.0ax will generate

incorrect code when the sample program below is compiled with loop

optimization (/Ol).

CAUSE

The problem stems from the combination of /Ol and the use of an

indexing variable local to the function inside the loop.

RESOLUTION

To work around this problem, do one or more of the following:

1. Declare the index variable as global, static, or volatile.

2. Do not use loop optimization (/Ol).

3. Compile with the /qc (quick compile) option.

STATUS

Microsoft has confirmed this to be a problem in C versions 6.0a and

6.0ax. This problem was corrected in C version 7.0.

More Information:

The following incorrect output occurs when the sample program below is

compiled with /Ol:

a[0] = -5

a[1] = 10

a[0] = 25637

a[1] = 8253

a[0] = 8285

The correct output should appear as follows:

a[0] = -5

a[1] = 5

a[0] = -5

a[1] = 5

a[0] = -5

Sample Code

-----------

/* Compile options needed: /Ol

*/

#include <stdio.h>

int a[2] = {-5,5};

int i;

void main(void)

{

int b = 1;

for ( i = 0 ; i < 5 ; i++ )

{

b = 1 - b;

printf("a[%d] = %d\n", b , a[b]);

}

}

Additional reference words: 6.00a 6.00ax