Reply to post: Re: Good advice but

Carnegie-Mellon Uni emits 'don't be stupid' list for C++ developers

bombastic bob Silver badge
Devil

Re: Good advice but

not using (...) and not allowing unsigned integers to 'wrap' is short-sighted...

the implications of 'printf'-like utilities, as well as gcc format checking pragma, were pointed out in the comments. But there are use cases for wrapping an unsigned integer, SUCH AS the calculation of a time interval on a 32-bit unsigned value that calculates milliseconds or microseconds, and wanting to schedule events based on elapsed time. When you EXPECT a wrap-around, you can code around it.

example:

uint32_t lTick = millis(); // # of milliseconds since start, using 32-bit unsigned value

...

if((int32_t)(millis() - lTick)) > my_interval) { do something; lTick += my_interval; }

this pretty much works universally, and is similar to what the Linux kernel does when scheduling things based on 'jiffies'.

(and in some cases I'll even truncate the math down to 16-bits to make code work faster, such as on a microcontroller like Arduino, where this example might be used a LOT)

so... maybe NO UNHANDLED unsigned integer wrapping?

(yeah a FEATURE, not a bug - I like to work WITH the system's limitations, not against them)

POST COMMENT House rules

Not a member of The Register? Create a new account here.

  • Enter your comment

  • Add an icon

Anonymous cowards cannot choose their icon

Biting the hand that feeds IT © 1998–2019