News for hackers. Stuff that matters.
Re:How to C in 2016 by Nursie on Friday January 08, 2016 @08:51AM (#10864778)
>> - 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.

Re:How to C in 2016 by IshKebab on Friday January 08, 2016 @09:47AM (#10865172)
Yeah 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.

Re:How to C in 2016 by annathebannana on Friday January 08, 2016 @10:04AM (#10865305)
Do 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.

Re:How to C in 2016 by comex on Friday January 08, 2016 @11:04AM (#10865804)
For one, if you write uint_least16_t, you're implicitly warranting that the code does the right thing if it isn't 16 bits, which means you have to think about it. And C's integer conversion rules and overflow restrictions are typically quite a lot to think about already... Not the strongest argument, but I think there is a case for applying YAGNI.

Also, it's less typing. :)

Re:How to C in 2016 by masklinn on Friday January 08, 2016 @10:11AM (#10865358)
> 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.

Re:How to C in 2016 by annathebannana on Friday January 08, 2016 @10:19AM (#10865430)
Both 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.

Re:How to C in 2016 by masklinn on Friday January 08, 2016 @11:31AM (#10866034)
> 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.

Re:How to C in 2016 by boomlinde on Friday January 08, 2016 @12:25PM (#10866519)
Well, 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.

Re:How to C in 2016 by masklinn on Friday January 08, 2016 @12:27PM (#10866534)
> 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.

INT_MAX (or INT_LEASTN_MAX for annathebannana's suggestion) doesn't require exploiting overflow behaviour, but going from 2^15 iterations to 2^31 or 2^61 iterations may be problematic.

Re:How to C in 2016 by boomlinde on Friday January 08, 2016 @12:58PM (#10866766)
Problematic, yes, but not the difference between a terminating loop and an endless loop, which is what he said was the result of the size change. If that actually is the case I'd guess the problem was that the compiler for the latter platform was removing a zero comparison loop condition based on the assumption that a value that only ever increments can only ever be zero once, using the unspecified nature of ocerflows to its advantage, while the former did not.

And using INT_MAX or similar for what sounds like a timing loop is a whole other can of bad practice. Then the problem isn't that you used the wrong type, it's that you used the wrong value.

Re:How to C in 2016 by masklinn on Friday January 08, 2016 @01:09PM (#10866862)
> Problematic, yes, but not the difference between a terminating loop and an endless loop

If the loop does significant work and was calibrated for an expectation of 65k iterations, stepping to 2 billion (let alone a few quintillion) is for all intents and purpose endless.

> And using INT_MAX or similar for what sounds like a timing loop is a whole other can of bad practice. Then the problem isn't that you used the wrong type, it's that you used the wrong value.

No objection there, doing that is making invalid assumptions, my point is that moving to exact-size integral does fix it.

Re:How to C in 2016 by boomlinde on Saturday January 09, 2016 @07:06AM (#10870971)
> If the loop does significant work and was calibrated for an expectation of 65k iterations, stepping to 2 billion (let alone a few quintillion) is for all intents and purpose endless.

And if it's not, it's not, the point being that endless in this case would be meaningless outside it's literal meaning unless we know more about the specific case.

> No objection there, doing that is making invalid assumptions, my point is that moving to exact-size integral does fix it.

No, using an exact value fixes it. Any unsigned integer type is just fine for any integer value from 0 to 65535. If you change the type to a larger integer type without changing the supposed iteration count, the code would not have this problem, and if you changed the value to something higher than 65535 without adjusting the size of the type, you would have a different problem. Thus, the problem described here does not pertain to the type of the variable used.

Re:How to C in 2016 by aidenn0 on Friday January 08, 2016 @12:25PM (#10866521)
The only issue I can think of is that arithmetic on an uint16_t will be signed on a 32-bit target and unsigned on a 16 bit target. I think the only possible way to get undefined behavior would be to left shift by enough to overflow the signed integer on the 32-bit target, which should be rare (and might not be well defined on the 16 bit target; I don't have the spec in front of me right now).
Re:How to C in 2016 by zAy0LfpBZLC8mAC on Friday January 08, 2016 @09:23AM (#10865003)
You "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.
Re:How to C in 2016 by Nursie on Friday January 08, 2016 @12:25PM (#10866511)
Oh 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.
Re:How to C in 2016 by ori_b on Friday January 08, 2016 @02:25PM (#10867483)
Yes, and the range of array indexes should be 'from 0 to the maximum index of the array'. This is system dependent.
Re:How to C in 2016 by annathebannana on Friday January 08, 2016 @10:02AM (#10865283)
[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

Re:How to C in 2016 by Nursie on Friday January 08, 2016 @12:23PM (#10866498)
>> [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.

Re:How to C in 2016 by to3m on Friday January 08, 2016 @01:50PM (#10867196)
The sized types are optional, since they are more restrictive than the ordinary types - see section 7.20.1.1 in the standard (http://port70.net/~nsz/c/c11/n1570.html#7.20.1.1). To summarize: intN_t is exactly N bits, with zero padding bits, and 1 2's complement sign bit; uintN_t is exactly N bits, with zero padding bits; the implementation must provide such typedefs for all such types it supports.

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.

Re:How to C in 2016 by bsder on Friday January 08, 2016 @09:41PM (#10869837)
> So, if a system is 1's complement

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?

Re:How to C in 2016 by to3m on Friday January 08, 2016 @10:11PM (#10869919)
I am playing the language lawyer game - there is nothing to call out. Is what I say justified by the standard? Yes, or no? There are no other issues involved ;)

I have no idea if C99 runs on any 1's complement systems. I don't know what end-around carry is. I don't care what it is. I don't care about any of this 1's complement nonsense. But the standard says it exists, and must be taken into account, and, therefore, by the rules of the game, I am obliged to assume these things.

Re:How to C in 2016 by bluetomcat on Friday January 08, 2016 @10:09AM (#10865342)
What 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.
Re:How to C in 2016 by annathebannana on Friday January 08, 2016 @10:21AM (#10865446)
If you are sure that this is going to be the case, then use uint_least8_t.
Re:How to C in 2016 by _wmd on Friday January 08, 2016 @08:55AM (#10864791)
Assuming 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

Re:How to C in 2016 by Nursie on Friday January 08, 2016 @08:56AM (#10864802)
>> 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.