by Nursie · 10y ago · 0 comments
Can you explain this? I'm not seeing it.
You should generally know the range of numbers you're seeking to store in an integer variable, if you don't then you're asking for trouble.
I ran into a bug where I was running some Arduino code (16-bit) on an mBed (32-bit). The use of `int` rather than `uint16_t` caused a timeout to become an infinite loop.
I'm pretty sure it would be impossible to have portability issues due to using fixed-width types - I mean their entire point is to ensure portability.
Their point is against portability as they may not exist at all.
Also, it's less typing. :)
The good part being that you're warned at compile time, not when your 2^16 elements loop becomes a 2^32 elements loop.
Or if you do not make assumptions of the sizes of int, something that only bad programmers do.
in 99% of the cases, except if you are working with network protocols, [u]intN_t is a bad choice
It will, that's guaranteed by the standard. If it's not a standards compliant compiler/runtime/platform, you've got bigger problems.
>> If you are working with arrays, always use size_t if not, use [u]int_leastN_t
Sure, on array indices, size_t is appropriate.
I can't see an advantage in using 'least' as standards dictate that 8/16/32/64 bit types are available.
>> in 99% of the cases, except if you are working with network protocols, [u]intN_t is a bad choice
No, it's the best choice in most cases because it makes the code a little more explicit and easier to understand, and it makes developers think about the range of the data you're using.
So, if a system is 1's complement, you won't get the intN_t types at all. If the system doesn't support a particular bit width, you won't get the types for that width.
As someone who actually programmed a 1's complement system with end-around carry, I'm going to call you out on this.
What system still exists that can run C99 and is one's complement?
You can still range check the natural integer types, just use INT_MAX and suchlike
Either you need the range, in which case you're stuck with a large type, or you don't and can use a smaller one.
I'm not sure where 'oversized' comes into it.