Loop Optimization Problem with Pointers in C 5.1

ID Number: Q44002

5.10 | 5.10

MS-DOS | OS/2

buglist5.10 fixlist6.00

Summary:

When the following code is compiled with C version 5.1 with time

optimization (/Ot) only or loop optimization (/Ol) only, the resulting

output is incorrect. If you compile with loop and/or time optimization

and alias checking, the correct output is given.

Note: The right-most column of addresses (pointer values) should

increase if the program runs correctly. In Microsoft C version 5.1,

these numbers stay constant.

More Information:

Because the default optimization is /Ot, incorrect code is generated

with the default optimization. /Ol will also fail. Correct code is

generated with /Odat, /Odal, /Odalt, and /Ox.

Microsoft has confirmed this to be a problem in C version 5.1. This

problem was corrected in C version 6.0.

Sample Code

-----------

/* Compile options needed: /Ol

*/

#include <stdio.h>

struct Block{

struct Block *fwp;

};

main ()

{

struct Block *at,*aa;

char *c;

int i;

at=NULL;

for ( i = 0 ; i < 10 ; i++ ){

aa = (struct Block *)malloc(sizeof(struct Block));

if ( !aa ) break;

if (at){

aa->fwp=at->fwp;

at->fwp=aa;

}

else {

at=aa;

aa->fwp=aa;

}

printf("at=%d aa=%d at->fwp=%d aa->fwp=%d\n",

at,aa,at->fwp,aa->fwp);

}

}