-deleted-
9 years ago.

Prevent data overflow errors

What is the best way to prevent data overflow and stop an MCU lockup or crash due to an overflow?

Is there a way to have an error handler trap the program location of the overflow with?

  try {
      // code causing possible data overflow here 
  }
  catch (int n) {
      throw 221; // use a unique error number (221) to help find the location of the error

  }

.

C++ exceptions: http://www.cplusplus.com/doc/tutorial/exceptions/

Can you explain what you mean by data overflow? As in, I can imagine situations where something overflows, but how it matters, and if it matters at all, really depends on your specific situation.

posted by Erik - 27 Apr 2015

Sure; data overflow would occur when using an int16_t data type and the value exceeds 32,767 due to a calculation or a value sent over Ethernet or Xbee radio.

What happens when this occurs and int16_t > 32,767 ?

This may point to the need for data validation and pseudo logic to prevent the data overflow:

if (var_int16_t > 32766) {
  var_int16_t  = 32766;
}

.

Data Types: http://developer.mbed.org/handbook/C-Data-Types

posted by -deleted- 27 Apr 2015

Yeah but how to handle that is really specific for your situation. For example the mbed timer overflows all the time (well once every hour). No problem since the substractions still work fine even with overflow. If you use Ethernet and it is important that values arrive correctly, just use a TCP connection. In general calculation errors/wrong values are then the problem, not if they would cause an overflow.

posted by Erik - 27 Apr 2015
Be the first to answer this question.