I have a sketch containing the following method:
// Writes zeros to the complete screen clearing it:
void clearScreen(uint8_t val) {
setDrawArea(0x00, 0x7f, 0x00, 0x07); // complete screen
SSD1306.ssd1306_send_data_start();
for (int c = 0; c < 128 * 8; c++) {
SSD1306.ssd1306_send_byte(val);
}
SSD1306.ssd1306_send_data_stop();
}
The whole sketch compiles with a size of 6730 Bytes. While refactoring the code I mistakenly changed "c" to uint8_t. After this I compiled to 4124 Bytes. Does this come from the compiler recognizing that the loop condition will never be reached and cut off all code behind or is this some kind of strange optimization event? I'm asking because I can't test the sketch in the moment.
uint_8
, which uses less space)c < 128 * 8;
results in theuint_8
variable 'c' to be overflowed. The result is undefined behavior. With undefined behavior, anything can happenuint8_t
is perfectly safe and well defined. It just rolls over modulo 256. Only the signed integer overflows are undefined behavior.c
were signed, making the overflow an issue, that comparison doesn't cause an overflow, the incrementc++
might.