VC7.1 For followed by If compiles wrong

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am using Visual C++ .NET 2003. I have discovered the following problem and
did not find any other mention of it on the forums.

This code was being run in Debug mode, console application, with no changes
to the default project settings.

A basic overview of the problem is this: the two following sets of code
SHOULD compile and execute in the same way but they do NOT.

for (int i=0; i<256; i++)
if (table == *string) break;

for (int i=0; i<256; i++) {
if (table == *string) break;
}

Table is a static character array [256] and string is const char*.

In the first set of code, my program runs right over it, exiting with i = 0.
In the second set, it will exit with i = 116. You can download my exact
code from the following web address.

http://simsof.united.net.kg/BugCode.zip
 
Sorry, I might be wrong about this. What is happening is the IDE is
(incorrectly?) showing the yellow arrow to indicate which line is executed
next and it places this yellow arrow on the wrong instruction. Also in the
disassembly I only looked so far as to see the JNE moved execution down to
the first ASM line after output->WriteChar which had me confused too, I
thought that was part of the output->WriteChar instruction and not a return
into the For.

<code>
for (i = 0; i<256; i++)
00413A04 mov dword ptr ,0
00413A0E jmp Burrows::PerformMTF+8Fh (413A1Fh)
00413A10 mov eax,dword ptr
00413A16 add eax,1
00413A19 mov dword ptr ,eax
00413A1F cmp dword ptr ,100h
00413A29 jge Burrows::PerformMTF+0B7h (413A47h)
if (table == *string) break;
00413A2B mov eax,dword ptr
00413A31 movsx ecx,byte ptr table[eax]
00413A39 mov edx,dword ptr [string]
00413A3C movsx eax,byte ptr [edx]
00413A3F cmp ecx,eax
00413A41 jne Burrows::PerformMTF+0B5h (413A45h)
00413A43 jmp Burrows::PerformMTF+0B7h (413A47h)
output->WriteChar(i);
00413A45 jmp Burrows::PerformMTF+80h (413A10h)
00413A47 mov al,byte ptr
00413A4D push eax
00413A4E mov ecx,dword ptr [output]
00413A51 call Buffer::WriteChar (41131Bh)
</code>
 
mosimu said:
A basic overview of the problem is this: the two following sets of code
SHOULD compile and execute in the same way but they do NOT.

for (int i=0; i<256; i++)
if (table == *string) break;

for (int i=0; i<256; i++) {
if (table == *string) break;
}


I'm not sure what your problem is here; I've never experienced it. My
first suspicion was that it was because VC++, by default, uses
non-standard scoping rules for variables declared in for initializers.
However, these non-standard rules should apply in both of the cases
above. If you could post both complete programs, I could attempt to
reproduce it.
 
mosimu said:
output->WriteChar(i);
00413A45 jmp Burrows::PerformMTF+80h (413A10h)

Oops, I see your problem now, and I have reproduced it.

The essential issue is that the IDE needs to indicate somehow that you
are at the end of the loop, at the closing brace, and about to go back.
Confusingly, when a closing brace wasn't around, it decided to indicate
this by assigning that position to the follow line of code. Here's a
quite minimal program with the same issue:

int main()
{
for (int i=0; i<256; i++)
if (i == -1) break;
return 0;
}

I've reproduced the issue even in the most recent version of Visual
Studio. Although it has pretty much always worked this way, your
confusion is perfectly reasonable, and I will contact someone about it.
Thanks for reminding us.
 
Back
Top