473,698 Members | 2,528 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

VC7.1 For followed by If compiles wrong

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[i] == *string) break;

for (int i=0; i<256; i++) {
if (table[i] == *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

Nov 17 '05 #1
3 1049
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 [i],0
00413A0E jmp Burrows::Perfor mMTF+8Fh (413A1Fh)
00413A10 mov eax,dword ptr [i]
00413A16 add eax,1
00413A19 mov dword ptr [i],eax
00413A1F cmp dword ptr [i],100h
00413A29 jge Burrows::Perfor mMTF+0B7h (413A47h)
if (table[i] == *string) break;
00413A2B mov eax,dword ptr [i]
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::Perfor mMTF+0B5h (413A45h)
00413A43 jmp Burrows::Perfor mMTF+0B7h (413A47h)
output->WriteChar(i) ;
00413A45 jmp Burrows::Perfor mMTF+80h (413A10h)
00413A47 mov al,byte ptr [i]
00413A4D push eax
00413A4E mov ecx,dword ptr [output]
00413A51 call Buffer::WriteCh ar (41131Bh)
</code>
Nov 17 '05 #2
mosimu wrote:
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[i] == *string) break;

for (int i=0; i<256; i++) {
if (table[i] == *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.
--
Derrick Coetzee, Microsoft Speech Server developer
This posting is provided "AS IS" with no warranties, and confers no
rights.
Nov 17 '05 #3
mosimu wrote:
output->WriteChar(i) ;
00413A45 jmp Burrows::Perfor mMTF+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.
--
Derrick Coetzee, Microsoft Speech Server developer
This posting is provided "AS IS" with no warranties, and confers no
rights. Use of included code samples are subject to the terms specified
at http://www.microsoft.com/info/cpyright.htm
Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
1860
by: Bimo Remus | last post by:
Hi, I'm having problems with an assignment. We're supposed to take an old assignment and rewrite it so that each task is carried out by a different function. I just started this afternoon but have already run into a wall. When I try to compile the program, it seems to work, but after the first cin, an error message pops up and it crashes. It might have something to do with the pointers, seeing how I am still very shaky on them (I'm...
1
2034
by: Jozsef Bekes | last post by:
Hi All, I need to derive a class in VB.Net from a COM interface declared in an idl file in a VC++ project. Here is the interface declaration: ----------------------------------------------------- interface IParameter : IDispatch { HRESULT _stdcall ID( long* );
2
1100
by: bholland | last post by:
I have an ASP.NET 2.0 project that has known bad code: public page_load(object sender, EventArgs e) { foorbar(); } There is no reference to foobar() ANYWHERE. I do a build in Visual Studio and it compiles.
2
1860
by: Rui Maciel | last post by:
I've been fooling around with a test class and meanwhile I stumbled on a pointer problem. I have two classes, one being nested in the other. I've wrote a method in the nested class that would set a nested class's member pointer as a pointer to an instance of the other class. Here is the code: <code> template <class tData> class Foo
0
8680
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8609
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9169
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8871
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6528
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5861
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3052
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2335
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.