For loop Error - by Lenram

Status : 

  Fixed<br /><br />
		This item has been fixed in the current or upcoming version of this product.<br /><br />
		A more detailed explanation for the resolution of this particular item may have been provided in the comments section.


1
1
Sign in
to vote
ID 729818 Comments
Status Closed Workarounds
Type Bug Repros 0
Opened 3/8/2012 12:58:28 PM
Access Restriction Public
Moderator Decision Sent to Engineering Team for consideration

Description

The following piece of  "C" language code suppose to print K = 0 Ctr = 5
But its printing K = 0 C = 1.  This is happening in "Release" configuration. Its printing correctly in Debug mode.

=====================
int  k = 0;
int Ctr = 0;

			for (k = 0; k < 5; k++)
			{

				if(k == 0 )
				{
					printf("K = 0");					

				}
				Ctr++;								
			}
                        printf(" Ctr =  %d \n",iCtr );


==================

Note: - I tested in VS 2008. I hope it will happen in 2010 too.
Sign in to post a comment.
Posted by Microsoft on 3/22/2012 at 2:36 PM
Hi, thanks for this report. This is a bug in the optimizer, and will be fixed in the release of VS11. If you are curious, here are the details.

There is an optimization called if-unswitching that turns:

for (i=L; i<U; i++) {
if (i == 3)
     // do something
}

Into this (removing the for-loop entirely):

if (L <= 3 < U) {
i=3;
// do something
}

The bug is that we were not considering uses of 'i' beyond the loop body when performing if-unswitching. This will be fixed in VS11.

Thanks!
Eric
Microsoft VC++ Team
Posted by MS-Moderator09 [Feedback Moderator] on 3/8/2012 at 10:01 PM
Thanks for your feedback. We are routing this issue to the appropriate group within the Visual Studio Product Team for triage and resolution. These specialized experts will follow-up with your issue.Thank you !
Posted by MS-Moderator01 on 3/8/2012 at 1:58 PM
Thank you for your feedback, we are currently reviewing the issue you have submitted. If this issue is urgent, please contact support directly(http://support.microsoft.com)