go_away

Author Topic: While(1)  (Read 2168 times)

0 Members and 1 Guest are viewing this topic.

Offline KlingerTopic starter

  • Jr. Member
  • **
  • Posts: 8
  • Helpful? 0
While(1)
« on: March 11, 2009, 06:10:37 AM »

I my program I have a few While(1) statement but they are not staying in a infinite loop. I don't see any way they are break out of the statement

could you some reason why they are not staying in a infinite loop. Or other ways I can make a infinite loop I have tried for(;;) but that still did not work.

Thanks

Offline paulstreats

  • Supreme Robot
  • *****
  • Posts: 1,381
  • Helpful? 21
Re: While(1)
« Reply #1 on: March 11, 2009, 06:39:22 AM »
what is your setup? which microcontroller and which C compiler.

can you post an example of your code

Offline tristantech

  • Jr. Member
  • **
  • Posts: 34
  • Helpful? 0
Re: While(1)
« Reply #2 on: March 11, 2009, 07:50:13 PM »
Your compiler might not use while(1)... if you use gcc/winavr it should. Try while(1==1). That has the same purpose and should work no mater what.
Hey! Visit my website @ http://www.tristantech.net

Offline chelmi

  • Supreme Robot
  • *****
  • Posts: 496
  • Helpful? 15
Re: While(1)
« Reply #3 on: March 12, 2009, 09:05:03 AM »
Your compiler might not use while(1)... if you use gcc/winavr it should. Try while(1==1). That has the same purpose and should work no mater what.

 ??? what do you mean "not use while(1)" ? assuming we are talking about C, if your compiler does not loop for ever with this syntax then it's a bug in the compiler.

Offline Trumpkin

  • Supreme Robot
  • *****
  • Posts: 1,177
  • Helpful? 5
Re: While(1)
« Reply #4 on: March 12, 2009, 01:07:08 PM »
Your code should look like this:
Code: [Select]
while(1)
{
code here
}

you could also try
Code: [Select]
for(;;)
{
code here
}
« Last Edit: March 12, 2009, 01:08:55 PM by Trumpkin »
Robots are awesome!

Offline sprince09

  • Jr. Member
  • **
  • Posts: 16
  • Helpful? 0
Re: While(1)
« Reply #5 on: March 13, 2009, 12:50:41 PM »
Also make sure you don't have any interrupts running that somehow branch to a different part of your program.

Such as:

while(1){
      //main loop
}

void example_function(void){
    while(1){//do stuff
}

SIGNAL(interupt_vector){
    example_function();
}

Offline paulstreats

  • Supreme Robot
  • *****
  • Posts: 1,381
  • Helpful? 21
Re: While(1)
« Reply #6 on: March 13, 2009, 03:56:06 PM »
If you are using PICs make sure the watchdog timer is switched off

Offline Admin

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,632
  • Helpful? 169
    • Society of Robots
Re: While(1)
« Reply #7 on: March 19, 2009, 03:41:04 AM »
Quote
I my program I have a few While(1) statement but they are not staying in a infinite loop. I don't see any way they are break out of the statement
If you are using AVR, make sure you turn off optimization in the makefile. An empty while loop would otherwise be deleted when compiled (as it is useless).

Offline chelmi

  • Supreme Robot
  • *****
  • Posts: 496
  • Helpful? 15
Re: While(1)
« Reply #8 on: March 19, 2009, 08:42:42 AM »
Quote
I my program I have a few While(1) statement but they are not staying in a infinite loop. I don't see any way they are break out of the statement
If you are using AVR, make sure you turn off optimization in the makefile. An empty while loop would otherwise be deleted when compiled (as it is useless).

Are you sure? Which compiler are you referring to? I just checked with gcc 3.2.3 on x86, with -O3 and -Os and as I would expect the while(1); loop was not removed.
I am very doubtful that any decent compiler would remove this statement, since this would change the execution flow of the program. A while(1); statement is NOT useless.

On the contraey, while(0); will probably be optimized out, since this is clearly useless.

Chelmi.

Offline dellagd

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 730
  • Helpful? 5
  • Come to the dark side... We have cookies!
Re: While(1)
« Reply #9 on: March 19, 2009, 06:21:25 PM »
this is a bit more complex but it should work to as I see it:

Code: [Select]
code: //yes, I really want you to type "code"
//put your code here
goto code;

this should also be an infinite loop and should cancel out the chance of the compiler deleting the part of an infinite loop that makes it a infinite loop
Innovation is a product of Failure, which leads to Success.

If I helped, +1 helpful pls

I Won!
3rd place! I'm taking $100

Offline chelmi

  • Supreme Robot
  • *****
  • Posts: 496
  • Helpful? 15
Re: While(1)
« Reply #10 on: March 20, 2009, 08:38:03 AM »
this is a bit more complex but it should work to as I see it:

Code: [Select]
code: //yes, I really want you to type "code"
//put your code here
goto code;

this should also be an infinite loop and should cancel out the chance of the compiler deleting the part of an infinite loop that makes it a infinite loop

I know I sound repetitious, but no compiler will "delete the part of an infinite loop that makes it a infinite loop". I'm pretty sure his problem is not with the while(1) loop. Probably a bad interrupt handler or a watchdog timer, but without more info it's hard to tell.

Offline Webbot

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 2,121
  • Helpful? 108
Re: While(1)
« Reply #11 on: March 21, 2009, 08:27:38 AM »
I agree: the compiler wont get rid of a while(1){} but it would get rid of an empty for loop: eg  for(int i=0; i<100;i++){}  would get removed by optimisation.

Are you sure its jumping out of the loop - or is the microcontroller reseting and starting from the beginning again?
Webbot Home: http://webbot.org.uk/
WebbotLib online docs: http://webbot.org.uk/WebbotLibDocs
If your in the neighbourhood: http://www.hovinghamspa.co.uk

Offline frank26080115

  • Supreme Robot
  • *****
  • Posts: 322
  • Helpful? 2
Re: While(1)
« Reply #12 on: March 24, 2009, 09:23:10 AM »
If it's an AVR chip, what are you doing inside this loop? make sure nothing touches your stack memory or else RET or RETI instructions will get confused (no out of bound array access and use free() for every malloc() and calloc()). Check the .lss file generated to see if the RJMP is correct and there's no possible path that can lead to the RJMP being skipped. If you can, run the code in the simulator. Also make sure your circuit is not noisy, a bad clock cycle might cause you to skip instructions.

Offline fiflak666

  • Jr. Member
  • **
  • Posts: 22
  • Helpful? 0
  • My native language isn't english :P
Re: While(1)
« Reply #13 on: March 24, 2009, 12:46:49 PM »
Quote
I my program I have a few While(1) statement but they are not staying in a infinite loop. I don't see any way they are break out of the statement
If you are using AVR, make sure you turn off optimization in the makefile. An empty while loop would otherwise be deleted when compiled (as it is useless).

No way, while(1) is never useless and compiler that doesn't know this is a weak compiler ;) there is no other way to stop the C written program to step over a part of memory outside its border

Offline Admin

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,632
  • Helpful? 169
    • Society of Robots
Re: While(1)
« Reply #14 on: April 08, 2009, 05:59:46 AM »
my bad ;D

 


Get Your Ad Here