Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
9 years, 6 months 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 2015Sure; 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:
.
Data Types: http://developer.mbed.org/handbook/C-Data-Types
posted by -deleted- 27 Apr 2015Yeah 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