5 ms·
>> - Using fixed width integers in for loops seems like a fabulous way to reduce the portability of code Can you explain this? I'm not seeing it. You should g
by Nursie 11y ago
>> - Using fixed width integers in for loops seems like a fabulous way to reduce the portability of code
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.
- _wmd 11y agoAssuming running time is considered a feature of your project, oversized int types on smaller hardware will adversely effect that You can still range check the natural integer types, just use INT_MAX and suchlike
- Nursie 11y ago>> Assuming running time is considered a feature of your project, oversized int types on smaller hardware will adversely effect that 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.
- zAy0LfpBZLC8mAC 11y agoYou "know the range", but not necessarily in terms of concrete numbers. Often, you iterate over variable size data structures, for example. All you know is that it won't be larger than the C implementation it's being compiled with allows--which usually roughly means that it won't be larger than the amount of memory the processor it's running on can address. But how much that actually is? Well, depends on the processor, and there also is no upper limit, as you always can invent a processor that can address more memory than any processor before. That's why you should usually use appropriate abstract types (such as size_t in this case) that the compiler will make sure are large enough to be able to iterate over any data structure you will encounter on that target.
- Nursie 11y agoOh sure, for array index stuff like that. For general iteration against another number, say, I would use a fixed (or fast) size appropriate to my data.
- IshKebab 11y agoYeah in fact using non-fixed width integers reduces portability! 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.
- annathebannana 11y agoDo you have any reason for supporting uint16_t instead of uint_least16_t? Their point is against portability as they may not exist at all.
- masklinn 11y ago> Their point is against portability as they may not exist at all. The good part being that you're warned at compile time, not when your 2^16 elements loop becomes a 2^32 elements loop.
- annathebannana 11y agoBoth problems are being avoided if you use [u]int_leastN_t Or if you do not make assumptions of the sizes of int, something that only bad programmers do.
- masklinn 11y ago> Both problems are being avoided if you use [u]int_leastN_t No you'll have the exact problem I cited in that case. > Or if you do not make assumptions of the sizes of int, something that only bad programmers do. Requesting a specific data size is the exact opposite of making assumptions.
- boomlinde 11y agoWell, the assumption you have typically been making, when your unsigned loop counter is causing problems because it is larger than expected, is that the counter will exhibit a particular behavior when it over- or underflows. IIRC, this is unspecified in the standard and you can't write portable code making such assumptions. I can see how the type changing to a smaller one might cause problems, but I don't see how IshKebab's example could happen without exploiting implementation specific overflow behavior.
- annathebannana 11y ago[u]intN_t may not exist. If you are working with arrays, always use size_t if not, use [u]int_leastN_t in 99% of the cases, except if you are working with network protocols, [u]intN_t is a bad choice
- bluetomcat 11y agoWhat if your array is never going to be larger than, say, UINT8_MAX, and you need to pack thousands of these "sizes" into an array of structs? Using an 8-byte size_t in that case will inflate the cache usage significantly.
- annathebannana 11y agoIf you are sure that this is going to be the case, then use uint_least8_t.
- Nursie 11y ago>> [u]intN_t may not exist. 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.
- exprI 11y ago>It will, that's guaranteed by the standard. If it's not a standards compliant compiler/runtime/platform, you've got bigger problems. No it isn't. It's a bad practice citing something you didn't read. Standard compliance can be achieved on platforms without native 8-, 16-, 32- or 64-bit integers (signed and/or unsigned), and you need not simulate such integers in software, which is why all the (u)intN_t types are optional.
- ori_b 11y agoYes, and the range of array indexes should be 'from 0 to the maximum index of the array'. This is system dependent.