Skip to main content
3 of 9
Added answers more clearly.
Ricardo
  • 3.4k
  • 2
  • 26
  • 55

First, a caveat. I'm a bit of a begginner in Arduino and AVR MCUs, so there is probably something innacurate in what I'll say. If that's the case, feel free to just edit, criticize or downvote. In any case, I wanted to go into a little more detail than others have gone with their ansewers.

So, let's see a few examples of what can go wrong.

Uninitialized variables

int status; pinMode(13, OUTPUT); digitalWrite(13, status); 

This problem is pretty harmless, but nobody knows whether the LED will light up or not. The value of variable status will be whatever happens to be the value that memory position was initialized with. But it's not defined in your program.

Memory overflow

int array[10]; int v = array[100]; array[-100] = 10; 

The first problem here is that you don't know what will be assigned to v, but worse is that you don't know what you messed up with the assignment to position -100 of array. The good news is that you only messed up with RAM and not with the EEPROM, thus your program is safe.

Jump to an illegal instruction

void doSomething( void ) { for (int i = 0; i < 1000; i++); } void setup () { void (*funcPtr)( void ); funcPtr = &doSomething; funcPtr(); // calls doSomething(); funcPtr = NULL; funcPtr(); // undefined behavior } 

The first call to funcPtr() will actually be a call to doSomething(). The second one will lead to undefined behavior. It will probably continue execution from memory address 0. What's there? The bootloader begins there? I don't know, need to read the compiler fine print. But certainly it is not the intended behavior.

Other bad things that may happen

Well, you can run out of RAM, for example. What else. In any case, I think your program will keep running, probably not the way you intended it to.

Kinds of Protection

In computer systems, problems like these are usually dealt with at various levels:

  1. By the compiler
  2. By the programming language runtime (as in Java for example).
  3. By the operating system or the processor (if your memory access a position outside the boundaries of the address space reserved to your program, the OS or the processor may have safety mechanisms to prevent that)

Arduinos only have only limited protection of the compiler, and probably nothing else. The good news is that they aren't multi-tasked, so the only program being affected is yours. In any case, any of those bugs will lead to erratic behavior.

The Answers

The assumptions are the all of the problems I stated above are runtime problems.

What happens if there is a runtime error in a program?

The program will continue and what happens will depend on the side-effects of the runtime error. A call to the null function pointer will probably (yes, I'm guessing) make the program jump to an unknown location. The assignment of an undetermined value to a variable will cause your program to use that value as a valid one, and gosh knows what it will do with that.

Will execution of the program just stop?

No, it will keep going as if nothing extraordinary happened, probably doing what you didn't intend it to do. It may reset or act erractically. It may turn some inputs into outputs and burn a sensor or two (well, that may be a bit unlikely, admittedly).

Is there some way I get the Arduino to tell me what the error is?

I don't think so. As I said earlier, the protection mechanisms aren't there. There's no runtime support from the language, no OS, no hardware checks for out-of-bounds memory access (the bootloader doesn't count as either). You just have to be careful with your program and probably set your own safety nets.

The reason for the lack of protection is probably because Arduino controllers are too cheap, have too little memory, and should not run anything too important (yes, there seems to be a disclaimer by AVR somewhere for you not to use the MCUs normally used by Arduino in life support systems).

Ricardo
  • 3.4k
  • 2
  • 26
  • 55