473,378 Members | 1,496 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Endless Loop in C-Code when Using /Og

We are using Visual Studio .NET 2003. When using that compiler, the
following example code goes into an endless loop in the "while" loop when the
/Og optimization option is used:

#include <stdlib.h>
int resize(int *incsize, int min_size) {
while(*incsize <= min_size) {
*incsize = (int)(*incsize * 1.25);
}
if ( min_size > 60 ) return 0;
else return min_size;
}
int main() {
int incsize = 10;
return resize(&incsize, 12);
}

The problem occurs when using a pointer and real value for the
multiplication in the while loop. It also requires some additional code
(other than just a return or print statement) after the loop.

To me, this looks like a compiler bug. I looked on Microsoft web site for
patches to Visual Studio .NET 2003 but I did not see any. Does anyone know
of a patch for this issue and if so, from where can I down load it?

Thanks,

Nov 17 '05 #1
13 1754
Hi Bev,

No solution for you here, but just a weird question here. Why is the while
loop there at all? The code would do exactly the same thing without it,
since it returns based only on the unchanged inputed parameter min_size (
incsize has no affect on the output exact possibly to hang in the while
loop). In fact, all the method resize() does is return '0' if min_size is
greater than 60, and min_size itself otherwise...

I'm guessing this is a simplification of your actual code, in which case I
understand that you're just boiling it down to a simpler thing that displays
the problem. But then why is the "if (m_size >60)..." code there, since it
is not part of the problem? In your example code the call to it should
return a 12 since 12 is less than or equal to 60...

[==P==]

"Bev in TX" <Bev in TX@discussions.microsoft.com> wrote in message
news:7A**********************************@microsof t.com...
We are using Visual Studio .NET 2003. When using that compiler, the
following example code goes into an endless loop in the "while" loop when
the
/Og optimization option is used:

#include <stdlib.h>
int resize(int *incsize, int min_size) {
while(*incsize <= min_size) {
*incsize = (int)(*incsize * 1.25);
}
if ( min_size > 60 ) return 0;
else return min_size;
}
int main() {
int incsize = 10;
return resize(&incsize, 12);
}

The problem occurs when using a pointer and real value for the
multiplication in the while loop. It also requires some additional code
(other than just a return or print statement) after the loop.

To me, this looks like a compiler bug. I looked on Microsoft web site for
patches to Visual Studio .NET 2003 but I did not see any. Does anyone
know
of a patch for this issue and if so, from where can I down load it?

Thanks,

Nov 17 '05 #2
That is an example program -- not the real thing. I pared out everything
that did not affect the bug.

Bev in TX
Nov 17 '05 #3
I should also have said that the real code reallocates memory after the loop
-- I just tried to remove everything that was complicated but still produce
the error.
Nov 17 '05 #4
aa
"Bev in TX" <Bev in TX@discussions.microsoft.com> wrote in message
news:7A**********************************@microsof t.com...
We are using Visual Studio .NET 2003. When using that compiler, the
following example code goes into an endless loop in the "while" loop when
the
/Og optimization option is used:

#include <stdlib.h>
int resize(int *incsize, int min_size) {
while(*incsize <= min_size) {
*incsize = (int)(*incsize * 1.25);
}
if ( min_size > 60 ) return 0;
else return min_size;
}
int main() {
int incsize = 10;
return resize(&incsize, 12);
}

The problem occurs when using a pointer and real value for the
multiplication in the while loop. It also requires some additional code
(other than just a return or print statement) after the loop.

To me, this looks like a compiler bug. I looked on Microsoft web site for
patches to Visual Studio .NET 2003 but I did not see any. Does anyone
know
of a patch for this issue and if so, from where can I down load it?


int incsize = 1;
incsize = (int) 1.25 * incsize;
if (incsize != 1)
{
std::cout << "I owe you a cup of coffe" << endl;
}
Nov 17 '05 #5
I see that you're pointing out that IF 'incsize' is given a value of 1
initially it will stay 1, which would produce an infinite loop. HOWEVER, if
you look closer at her code you'll see she sets 'incsize' intially to 10, so
it shouldn't be an infinite loop... : )

[==P==]

PS - Loved the 'I owe you a cup of coffee' code... : )

"aa" <ss> wrote in message news:%2****************@TK2MSFTNGP10.phx.gbl...
"Bev in TX" <Bev in TX@discussions.microsoft.com> wrote in message
news:7A**********************************@microsof t.com...
We are using Visual Studio .NET 2003. When using that compiler, the
following example code goes into an endless loop in the "while" loop when
the
/Og optimization option is used:

#include <stdlib.h>
int resize(int *incsize, int min_size) {
while(*incsize <= min_size) {
*incsize = (int)(*incsize * 1.25);
}
if ( min_size > 60 ) return 0;
else return min_size;
}
int main() {
int incsize = 10;
return resize(&incsize, 12);
}

The problem occurs when using a pointer and real value for the
multiplication in the while loop. It also requires some additional code
(other than just a return or print statement) after the loop.

To me, this looks like a compiler bug. I looked on Microsoft web site
for
patches to Visual Studio .NET 2003 but I did not see any. Does anyone
know
of a patch for this issue and if so, from where can I down load it?


int incsize = 1;
incsize = (int) 1.25 * incsize;
if (incsize != 1)
{
std::cout << "I owe you a cup of coffe" << endl;
}

Nov 17 '05 #6
My guess is that it's an optimization bug. The compiler probably doesn't
reload the variable since it's in a tight loop and it's probably getting
tricked by the pointer. The OP might try doing something like:

#include <stdlib.h>
int resize(int *incsize, int min_size) {
int i = *incsize;

while(i <= min_size) {
i = (int)(i * 1.25);
}

if ( min_size > 60 )
return 0;
else
return min_size;
}

int main() {
int incsize = 10;
return resize(&incsize, 12);
}

However, what the OP did should work so I'm thinking this is the result of
an over zealous optimizer. Since the value of incsize really never changes
(just what it points to is being changed) the compiler probably removes the
check. This optimization is documented in the help.

Tom

"Peter Oliphant" <po*******@RoundTripInc.com> wrote in message
news:e5**************@TK2MSFTNGP12.phx.gbl...
I see that you're pointing out that IF 'incsize' is given a value of 1
initially it will stay 1, which would produce an infinite loop. HOWEVER, if
you look closer at her code you'll see she sets 'incsize' intially to 10,
so it shouldn't be an infinite loop... : )

[==P==]

PS - Loved the 'I owe you a cup of coffee' code... : )

"aa" <ss> wrote in message news:%2****************@TK2MSFTNGP10.phx.gbl...
"Bev in TX" <Bev in TX@discussions.microsoft.com> wrote in message
news:7A**********************************@microsof t.com...
We are using Visual Studio .NET 2003. When using that compiler, the
following example code goes into an endless loop in the "while" loop
when the
/Og optimization option is used:

#include <stdlib.h>
int resize(int *incsize, int min_size) {
while(*incsize <= min_size) {
*incsize = (int)(*incsize * 1.25);
}
if ( min_size > 60 ) return 0;
else return min_size;
}
int main() {
int incsize = 10;
return resize(&incsize, 12);
}

The problem occurs when using a pointer and real value for the
multiplication in the while loop. It also requires some additional code
(other than just a return or print statement) after the loop.

To me, this looks like a compiler bug. I looked on Microsoft web site
for
patches to Visual Studio .NET 2003 but I did not see any. Does anyone
know
of a patch for this issue and if so, from where can I down load it?


int incsize = 1;
incsize = (int) 1.25 * incsize;
if (incsize != 1)
{
std::cout << "I owe you a cup of coffe" << endl;
}


Nov 17 '05 #7
As already noted, this tiny program was greatly simplified (to the point of
being stupid), because I only wanted to keep what causes the compilation
error to reveal itself. Even so there is no problem with the given data.

In the original code there is no danger of incsize being "1" -- the values
are much larger than what I put below (>10000).

Bev in TX

"aa" wrote:
int incsize = 1;
incsize = (int) 1.25 * incsize;
if (incsize != 1)
{
std::cout << "I owe you a cup of coffe" << endl;
}


Nov 17 '05 #8
You are correct -- it should not go into an infinite loop. In fact, if I
build in debug mode or any optimizations except /Og, then the problem goes
away.

"Peter Oliphant" wrote:
I see that you're pointing out that IF 'incsize' is given a value of 1
initially it will stay 1, which would produce an infinite loop. HOWEVER, if
you look closer at her code you'll see she sets 'incsize' intially to 10, so
it shouldn't be an infinite loop... : )

[==P==]

PS - Loved the 'I owe you a cup of coffee' code... : )

"aa" <ss> wrote in message news:%2****************@TK2MSFTNGP10.phx.gbl...
"Bev in TX" <Bev in TX@discussions.microsoft.com> wrote in message
news:7A**********************************@microsof t.com...
We are using Visual Studio .NET 2003. When using that compiler, the
following example code goes into an endless loop in the "while" loop when
the
/Og optimization option is used:

#include <stdlib.h>
int resize(int *incsize, int min_size) {
while(*incsize <= min_size) {
*incsize = (int)(*incsize * 1.25);
}
if ( min_size > 60 ) return 0;
else return min_size;
}
int main() {
int incsize = 10;
return resize(&incsize, 12);
}

The problem occurs when using a pointer and real value for the
multiplication in the while loop. It also requires some additional code
(other than just a return or print statement) after the loop.

To me, this looks like a compiler bug. I looked on Microsoft web site
for
patches to Visual Studio .NET 2003 but I did not see any. Does anyone
know
of a patch for this issue and if so, from where can I down load it?


int incsize = 1;
incsize = (int) 1.25 * incsize;
if (incsize != 1)
{
std::cout << "I owe you a cup of coffe" << endl;
}


Nov 17 '05 #9
Do you know of any Visual C/C++ .NET 2003 updates?

Thanks,
Bev in TX

"Tom Serface" wrote:
My guess is that it's an optimization bug. The compiler probably doesn't
reload the variable since it's in a tight loop and it's probably getting
tricked by the pointer. The OP might try doing something like:

#include <stdlib.h>
int resize(int *incsize, int min_size) {
int i = *incsize;

while(i <= min_size) {
i = (int)(i * 1.25);
}

if ( min_size > 60 )
return 0;
else
return min_size;
}

int main() {
int incsize = 10;
return resize(&incsize, 12);
}

However, what the OP did should work so I'm thinking this is the result of
an over zealous optimizer. Since the value of incsize really never changes
(just what it points to is being changed) the compiler probably removes the
check. This optimization is documented in the help.

Tom

Nov 17 '05 #10
Sounds like they will be doing some service packs:

http://www.microsoft-watch.com/artic...1883868,00.asp

But I don't know if this particular problem will be addressed. My guess is
that this is a really tricky thing to fix for global optimization since it
would have to either never do the optimization or somehow monitory that data
each pointer points to. In your case, you are actually modifying the data
pointed to in the actual loop so that should be better. If it were being
modified outside of the loop (I.E., another thread) you'd probably want to
make it volatile so that optimizer would not, um, optimize it.

FWIW, I tried this same test with VC++ 2005 and it did not hang so it may be
something they fixed for this release. It is strange that when you set the
/Og option (in the Command Line box) it gives you the message:

cl : Command line warning D9035 : option 'Og' has been deprecated and will
be removed in a future release

:o)

Tom

Program I compiled:

// LoopTest.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <stdlib.h>
#include <iostream>
using namespace std;

int resize(int *incsize, int min_size)
{
while(*incsize <= min_size) {
*incsize = (int)(*incsize * 1.25);
}

cout << "Done" << endl;
if ( min_size > 60 )
return 0;
else
return min_size;
}

int _tmain(int argc, _TCHAR* argv[])
{
int incsize = 10;
return resize(&incsize, 12);
}

"Bev in TX" <Be*****@discussions.microsoft.com> wrote in message
news:4A**********************************@microsof t.com...
Do you know of any Visual C/C++ .NET 2003 updates?

Thanks,
Bev in TX

"Tom Serface" wrote:
My guess is that it's an optimization bug. The compiler probably doesn't
reload the variable since it's in a tight loop and it's probably getting
tricked by the pointer. The OP might try doing something like:

#include <stdlib.h>
int resize(int *incsize, int min_size) {
int i = *incsize;

while(i <= min_size) {
i = (int)(i * 1.25);
}

if ( min_size > 60 )
return 0;
else
return min_size;
}

int main() {
int incsize = 10;
return resize(&incsize, 12);
}

However, what the OP did should work so I'm thinking this is the result
of
an over zealous optimizer. Since the value of incsize really never
changes
(just what it points to is being changed) the compiler probably removes
the
check. This optimization is documented in the help.

Tom

Nov 17 '05 #11
output to asm and check that. if your app is really small then finding your
code will be really simple because I think that your comments will be
pushed to the asm. so make comments, and output to asm. post here if you
need help reading it.
You are correct -- it should not go into an infinite loop. In fact, if I
build in debug mode or any optimizations except /Og, then the problem goes
away.

"Peter Oliphant" wrote:
I see that you're pointing out that IF 'incsize' is given a value of 1
initially it will stay 1, which would produce an infinite loop. HOWEVER,
if you look closer at her code you'll see she sets 'incsize' intially to
10, so it shouldn't be an infinite loop... : )

[==P==]

PS - Loved the 'I owe you a cup of coffee' code... : )

"aa" <ss> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
> "Bev in TX" <Bev in TX@discussions.microsoft.com> wrote in message
> news:7A**********************************@microsof t.com...
>> We are using Visual Studio .NET 2003. When using that compiler, the
>> following example code goes into an endless loop in the "while" loop
>> when the
>> /Og optimization option is used:
>>
>> #include <stdlib.h>
>> int resize(int *incsize, int min_size) {
>> while(*incsize <= min_size) {
>> *incsize = (int)(*incsize * 1.25);
>> }
>> if ( min_size > 60 ) return 0;
>> else return min_size;
>> }
>> int main() {
>> int incsize = 10;
>> return resize(&incsize, 12);
>> }
>>
>> The problem occurs when using a pointer and real value for the
>> multiplication in the while loop. It also requires some additional
>> code (other than just a return or print statement) after the loop.
>>
>> To me, this looks like a compiler bug. I looked on Microsoft web site
>> for
>> patches to Visual Studio .NET 2003 but I did not see any. Does anyone
>> know
>> of a patch for this issue and if so, from where can I down load it?
>
> int incsize = 1;
> incsize = (int) 1.25 * incsize;
> if (incsize != 1)
> {
> std::cout << "I owe you a cup of coffe" << endl;
> }
>



--
new
Nov 17 '05 #12
Bev in TX wrote:
We are using Visual Studio .NET 2003. When using that compiler, the
following example code goes into an endless loop in the "while" loop
when the /Og optimization option is used:

#include <stdlib.h>
int resize(int *incsize, int min_size) {
while(*incsize <= min_size) {
*incsize = (int)(*incsize * 1.25);
}
if ( min_size > 60 ) return 0;
else return min_size;
}
int main() {
int incsize = 10;
return resize(&incsize, 12);
}


Use the volatile keyword. Fixes it instantly:

#include <stdlib.h>

int resize(volatile int *incsize, int min_size) {
while(*incsize <= min_size) {
*incsize = (int)(*incsize * 1.25);
}
if ( min_size > 60 ) return 0;
else return min_size;
}
int main() {
int incsize = 10;
return resize(&incsize, 12);
}
--
Reginald Blue
"I have always wished that my computer would be as easy to use as my
telephone. My wish has come true. I no longer know how to use my
telephone."
- Bjarne Stroustrup (originator of C++) [quoted at the 2003
International Conference on Intelligent User Interfaces]
Nov 17 '05 #13
As noted in another newsgroup, this is also fixed in VC++ 2005, however it
warns that /Og has been deprecated so ...

Tom

"Reginald Blue" <Re***********@hotmail.com> wrote in message
news:dl**********@trsvr.tr.unisys.com...
Use the volatile keyword. Fixes it instantly:

#include <stdlib.h>

int resize(volatile int *incsize, int min_size) {
while(*incsize <= min_size) {
*incsize = (int)(*incsize * 1.25);
}
if ( min_size > 60 ) return 0;
else return min_size;
}
int main() {
int incsize = 10;
return resize(&incsize, 12);
}

Nov 17 '05 #14

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

Similar topics

1
by: Leo | last post by:
hi all how can i terminate/stop an endless python loop which i have started inside emacs with C-c C-c. ta, leo
4
by: Derek | last post by:
I am trying to refresh one datagrid after updating another datagrid. I can not call BindData for one under the other bind sub because it creates an endless loops on page load. Is there way to...
30
by: Skybuck Flying | last post by:
I was just trying to figure out how some C code worked... I needed to make a loop to test all possible values for a 16 bit word. Surprise Surprise... C sucks at it... once again :D lol... C is...
24
by: Tweaxor | last post by:
This has been puzzling me all this week. This is actually a homework assignment from last semesmter. But the teacher wouldn't tell us why certain things didn't work, but it didn't just work. My...
4
by: Mircea Pleteriu | last post by:
Hi all, Does anybody know how to configure the session so that the session will never expire?
73
by: Claudio Grondi | last post by:
In the process of learning about some deeper details of Python I am curious if it is possible to write a 'prefix' code assigning to a and b something special, so, that Python gets trapped in an...
4
by: cdrsir | last post by:
I have written a program like followings: #include <iostream.h> #include <fstream.h> class cClass { ofstream *fout; char ch;
1
by: cory6903 | last post by:
i am having a problem with an endless loop **int x; **a: **cout << "enter a #" << endl; **cin >> x **switch(x) **{ ** case 1:
6
by: uche | last post by:
This function that I have implemented gives me an infinite loop. I am trying to produce a hexdum program, however, this function is not functioning correctly.....Please help. void...
1
by: Emil Horowitz | last post by:
Hi, I knew this before, but seem to have forgotten all about it: How do I make the main window reload once, after I close the popupup window launched from the main window? Putting...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.