infinite loops

V

Vector

Is any infinite loop better than other? Is there any difference between
there efficiency?
 
P

Phlip

Vector said:
Is any infinite loop better than other? Is there any difference between
there efficiency?

To what use will you put the answer? A compiler will treat for(;;) and
while(true) the same. The second takes longer to type. Always optimize
programming time.

The second might trigger a "conditional expression is constant" warning of
some type.
 
I

Ian Collins

Vector said:
Is any infinite loop better than other? Is there any difference between
there efficiency?
Define better.

Which conveys the intent better? I'd say while( true ){}

Efficiency shouldn't be an issue.
 
R

red floyd

Vector said:
Is any infinite loop better than other? Is there any difference between
there efficiency?

I'm going to assume this is a joke.

Have you determined that the infinite loop is your bottleneck? How
long does your infinite loop take to complete? Have you benchmarked to
see if your infinite loop runs in aleph-null time versus aleph-one time?
 
T

Tomás

Vector posted:
Is any infinite loop better than other? Is there any difference between
there efficiency?


In practise, they are the same. Compilers will produce the exact same
machine code for these:

for (;;) { int k = 7; }

while (true) { int k = 7; }

do { int k = 7; } while (true);


In theory, the first one is fastest because there's no condition being
tested for each iteration of the loop.

I myself use for(;;).


-Tomás
 
G

Gaijinco

This is slightly off-topic but I found it intresting: What does this
code prints?

#include <iostream>

int main()
{
int i=1;

while(i>0)
++i;

std::cout << i;

return 0;
}
 
A

amt.dwivedi

Gaijinco said:
This is slightly off-topic but I found it intresting: What does this
code prints?

#include <iostream>

int main()
{
int i=1;

while(i>0)
++i;

std::cout << i;

return 0;
}
well answer is very obvious
it will print negative number that is shortest one.
reason : consider the same code with little modification for ur
understanding
int main()
{
char i=1;

while(i>0)
++i;

std::cout << int(i);
std::cin.get();

return 0;
}
print -128
i increments from 1 to 127 than incrementing further will make it 127
to -128
this is wat the range of char -128 to 127.
 
T

Tomás

Gaijinco posted:
This is slightly off-topic but I found it intresting: What does this
code prints?

#include <iostream>

int main()
{
int i=1;

while(i>0)
++i;

std::cout << i;

return 0;
}


As far as I know it's undefined behaviour for a signed integral type to go
past its limit.

In practise though, on a 32-Bit system, I'd hazard a guess that it'd be one
of the following:

0
-1
-4294967296


-Tomás
 
R

Rolf Magnus

well answer is very obvious
it will print negative number that is shortest one.
reason : consider the same code with little modification for ur
understanding
int main()
{
char i=1;

while(i>0)
++i;

std::cout << int(i);
std::cin.get();

return 0;
}
print -128
i increments from 1 to 127 than incrementing further will make it 127
to -128

It might do this, or it might do anything else. The overflow behavior of
signed integers is undefined.
 
J

Jack Klein

Is any infinite loop better than other? Is there any difference between
there efficiency?

How could one infinite loop be better than another? Could one be
infinitely more infinite than the other? If one ends before the other
does, the comparison was invalid because the one that ends was not an
infinite loop to begin with.
 
P

Phlip

Jack said:
How could one infinite loop be better than another? Could one be
infinitely more infinite than the other? If one ends before the other
does, the comparison was invalid because the one that ends was not an
infinite loop to begin with.

Haw haw.

That sophistry is either a play on James Kanze's latest post to the thread
"Infinite loop == undefined behaviour?"...

http://groups.google.com/group/comp.lang.c++.moderated/msg/aa69f9d5e793835b

....or a supernatural channeling of it, if you hadn't read it yet.
 
O

Old Wolf

Rolf said:
It might do this, or it might do anything else. The overflow behavior of
signed integers is undefined.

This is actually a case of out-of-range assignment: i is promoted
to int for the expression ++i, and the result is then reassigned
to i.
 
R

Rolf Magnus

Old said:
This is actually a case of out-of-range assignment:

i is promoted to int for the expression ++i, and the result is then
reassigned to i.

The other example that used int for i was doing what I wrote. Anyway, what
would be the behavior here? Is the value just truncated (modulo) or is it
UB too?
 
O

Old Wolf

Rolf said:
<nitpick>unless char is signed and sizeof(int)==sizeof(char)</nitpick>.

Of course :)
The other example that used int for i was doing what I wrote. Anyway, what
would be the behavior here? Is the value just truncated (modulo) or is it
UB too?

Being signed, I think it is a case of implementation-defined
behaviour or an i-d signal in C99. (
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top