So we had a field issue, and after days of debugging, narrowed down the problem to this particular bit of code, where the processing in a while loop wasn't happening :
// heavily redacted code
// numberA and numberB are both of uint16_t
// Important stuff happens in that while loop
while ( numberA + 1 == numberB )
{
// some processing
}
This had run fine, until we hit the uint16 limit of 65535. Another bunch of print statements later, we discovered that numberA + 1
had a value of 65536
, while numberB
wrapped back to 0
. This failed the check and no processing was done.
This got me curious, so I put together a quick C program (compiled with GCC 4.9.2) to check this:
#include <stdio.h>
#include <stdint.h>
int main()
{
uint16_t numberA, numberB;
numberA = 65535;
numberB = numberA + 1;
uint32_t numberC, numberD;
numberC = 4294967295;
numberD = numberC + 1;
printf("numberA = %d\n", numberA + 1);
printf("numberB = %d\n", numberB);
printf("numberC = %d\n", numberC + 1);
printf("numberD = %d\n", numberD);
return 0;
}
And the result was :
numberA = 65536
numberB = 0
numberC = 0
numberD = 0
So it appears that the result of numberA + 1
was promoted to uint32_t. Is this intended by the C language ? Or is this some compiler / hardware oddity?