Will the Arduino crash if it never completes the main loop()?

I have had some stability issues with a couple of programs that I have written and I was wondering never finishing the main loop() was causing the problem. Here is a simple program that demonstrates the problem.

void setup()
{
  pinMode(4,OUTPUT);
}

void loop()
{
  digitalWrite(4,HIGH);
  delay(100);
  digitalWrite(4,LOW);
  delay(100);
  loop();
   digitalWrite(4,HIGH);
  delay(1000);
  digitalWrite(4,LOW);
  delay(50);
  
  
}

This will blink an LED for around 5 minutes and then it stops blinking. Does not ever making to the end of the main loop() cause problems? If so, why does it cause problems?

void loop()
{
  ...
  loop();
}

It's not crashing because it doesn't reach the end of the loop, it's crashing because you have recursion without an exit condition so what you're seeing is the result of stack overflow.

Actually I think it is crashing because it can't reach the end of the loop, It keeps looping...

Doc

"To understand recursion you must first understand recursion."

Every time you call a function a whole load of stuff (including where you came from) is pushed on to the stack. When your leave the function (return, or reach the end) all that data is removed from the stack.

If a function calls itself repeatedly with no way of "returning" from itself to the "itself" that called it, then the data on the stack is just going to grow and grow and grow. Eventually the stack will smash in to the data for the variables in the running program - kind of like a car hitting a brick wall. It crashes.

Docedison: Actually I think it is crashing because it can't reach the end of the loop, It keeps looping...

Doc

void loop() {

  while(1) {
    // Blink an LED
  }

  Serial.println("End of Loop");
}

Never reaches the end of the loop but never crashes.

What does it matter if it reaches the end of loop or not?

int main(void)
{
        init();

#if defined(USBCON)
        USBDevice.attach();
#endif

        setup();

        for (;;) {
                loop();
                if (serialEventRun) serialEventRun();
        }

        return 0;
}

As you can see from the contents of main.cpp, when the loop() function is run it just runs again. (for(;;) is the same as while(1)).

What does it matter if it reaches the end of loop or not?

Quite a lot if you're using the serial port :P

wildbill: Quite a lot if you're using the serial port :P

If you are using serial events. Bit of a hack that. You can still use the serial port.

Robo_Smith: This will blink an LED for around 5 minutes and then it stops blinking.

3.41 minutes I would guess.

void loop()
{
  digitalWrite(4,HIGH);
  delay(100);
  digitalWrite(4,LOW);
  delay(100);
  loop();  // -->> recurses back into loop, consuming 2 bytes of stack

2048 bytes of RAM, where you use 2 bytes every 0.2 seconds, would take 204.8 seconds to use all the RAM. That's 3.41 minutes.

Just checked with my stopwatch. 3 minutes 24 seconds (3.4 minutes). I love it when theory and reality agree!

[quote author=Nick Gammon link=topic=112276.msg843783#msg843783 date=1341013811] Just checked with my stopwatch. 3 minutes 24 seconds (3.4 minutes). I love it when theory and reality agree! [/quote] What are the two bytes? The IP from where the jmp happened?

Yup. Return address (a stack filled with @loop).

Thanks for all of the reply's, they were very helpful and show me an area that I need to look into and understand better.