I'm wondering if it is allowed in C++ to use overflow on purpose.
In specific I want to increase a counter every cycle and when uint32_max
is reached start from 0
.
In my mind the easy way would be:
uint32_t counter = 0;
while(condition)
{
counter++;
...
}
or would you manually reset the counter?:
uint32_t counter = 0;
while(condition)
{
if(counter == uint32_max)
{
counter = 0;
}
else
{
counter++;
}
...
}
That is fine because you used uint32_t
. Unsigned integers do not overflow but wrap-around from the maximum value to zero. Such behavior is well-defined in C++, unlike signed integer overflow/underflow.