News for hackers. Stuff that matters.
Re:How to C in 2016 by ue_ on Friday January 08, 2016 @07:20AM (#10864344)
>If you find yourself typing char or int or short or long or unsigned into new code, you're doing it wrong.

int is a type that's at least 16 bits large. I can't see the problem with using it. If I specify that I want a type that's 32 bits large, my code won't run on 16 bit systems.

Why should I assume? Because the author doesn't believe in writing code that works on "20 year old systems"?

Re:How to C in 2016 by AndyKelley on Friday January 08, 2016 @07:42AM (#10864440)
If 16 bits is enough, then you should be using int16_t. If 16 bits is not enough, then you should not be using int.
Re:How to C in 2016 by DSMan195276 on Friday January 08, 2016 @08:21AM (#10864621)
Except not really. You should use an int_fast16_t, which is the fastest integer type that has at least 16 bits. There's no reason to force a 16-bit integer which may carry performance implications when it's not necessary.
Re:How to C in 2016 by AndyKelley on Friday January 08, 2016 @08:33AM (#10864678)
Here is an IRC log of a discussion[1] I had about this topic in the #musl IRC channel. The folks in this channel are the developers behind musl-libc[2]. The conclusion drawn is that some poor design decisions were made that make the fast types not worth using.

[1]: https://gist.github.com/andrewrk/ac66b24a0a202d87cea7

[2]: http://www.musl-libc.org/

Re:How to C in 2016 by DSMan195276 on Friday January 08, 2016 @09:00AM (#10864834)
Thanks for that, that's pretty interesting to read about. Admittedly, I'm not sure I've ever actually used a int_fastN_t type in my code - I just kinda assumed they served their purpose. Reading that log I can see the obvious problems with those types which I never realized since I never really looked into them that much. int_least16_t seems like a perfectly fine substitute though, I don't really know why int_leastN_t and int_fastN_t would be different in the first place.

That said, there's really no way to correctly pick the "perfect" integer size for every platform - There's just too many variables. IMO I much prefer to just use 'int' when I know the values are going to be small - smaller then a 16-bit value - and I don't care about the actual width. But obviously it's a hot topic for some - As long as you follow the standard in regards to it's size then it doesn't really matter what integer value you choose to use. I prefer saving the fixed-width integer types for situations when I know I need such a size.

Re:How to C in 2016 by ealloc on Friday January 08, 2016 @10:03AM (#10865293)
I'm not conviced. Maybe it really is that the wider type is faster, and int_fast16_t should be 64 bits. I just tried it out on a 64 bit system. The program is just a for loop.

If the loop variable is a int_fast16_t the loop increment step compiles to

    	addq	$1, -16(%rbp)
If it is int16_t it compiles to

    	movzwl	-4(%rbp), %eax
	addl	$1, %eax
	movw	%ax, -4(%rbp)
The 64 bit width version is shorter, and so conceivably faster. Performance seems the same though, but that's probably because this program isn't a proper benchmark.
Re:How to C in 2016 by Narishma on Friday January 08, 2016 @02:30PM (#10867527)
> The 64 bit width version is shorter, and so conceivably faster.

Without profiling there's no way to tell on modern processors.

Re:How to C in 2016 by nwmcsween on Saturday January 09, 2016 @01:13AM (#10870308)
Seriously, you think movzwl -4(%rbp), %eax addl $1, %eax movw %ax, -4(%rbp) will be on par with addq $1, -16(%rbp)
Re:How to C in 2016 by DSMan195276 on Saturday January 09, 2016 @10:59AM (#10871661)
Modern processors are pretty crazy beasts. He right in saying it's impossible to say for sure without some hard numbers or profiling.

Comparatively, while one is three instructions and the other is one, that single instruction is still doing everything the other three are doing. The only reason they aren't both one instruction seems to be that `addw` must not being a thing (Why, I don't know). Since the I/O done by both will be nearly identical, and the addition's should be very comparable in speed, it's not crazy to say they should perform basically the same. If you compiled with -O2 I bet you'd see almost identical code, since it would probably remove the memory access.

Re:How to C in 2016 by marcosdumay on Friday January 08, 2016 @09:36AM (#10865088)
And that, on all chips you can touch, without a fault, will be the same type as int. Unless you still have a Z80 around, that is.

Yes, the int is technically less portable. No, I don't see any reason to use that longer name.

Re:How to C in 2016 by DSMan195276 on Friday January 08, 2016 @01:03PM (#10866803)
I agree with you - As far as I'm concerned 'int' and 'int_least16_t'/'int_fast16_t' are basically the same thing, or at least close enough to the same thing that there's no reason not to use an int for standard simple cases. If I'm declaring a simple loop variable that's I know is going to have a small lower-bound, I just use an int. The chances are pretty good that 'int' maps to a good size for the platform, regardless of what platform it is and what it actually provides. 'int_least16_t' probably does too, but it's much more likely I have 'int' around then 'int_least16_t', and between the two 'int' is much more self-explanatory as to why I choose to use it.
Re:How to C in 2016 by officialtedleo on Friday January 08, 2016 @07:23AM (#10864363)
> If I specify that I want a type that's 32 bits large, my code won't run on 16 bit systems.

Why not?

Re:How to C in 2016 by ue_ on Friday January 08, 2016 @07:31AM (#10864389)
Nevermind, I'm wrong.

I've read that if one uses int64_t with a compiler that targets a 32 bit system, doing various operations on the int64_t variable will be much slower because more machine code instructions need to be generated.

The point stands: Why not use int if one is looking to target any architecture that C will run on?

Re:How to C in 2016 by Gankro on Friday January 08, 2016 @07:40AM (#10864429)
If 16 bits is enough, use an explicitly 16-bit integer. If you want something architecture-sized (e.g. because it's dealing with pointers/addresses), use things like `size_t` which makes the intent much more clear, and which are better defined.

In most places, if you're using an `int` for non-address-y stuff you're just wasting space on wider archs, or you're overflowing on less wide ones. The only exception off the top of my head is "I have an array of bytes, but want to bulk process more efficiently than u8 would allow". Things like `size_t` (or explicit SIMD types!) are still probably a better choice there, if only to simplify the rule to "just don't use int".

Re:How to C in 2016 by vardump on Saturday January 09, 2016 @02:19AM (#10870420)
> If 16 bits is enough, use an explicitly 16-bit integer.

This will often generate more code. So don't do this except when it saves a modest amount of memory.

Re:How to C in 2016 by nwmcsween on Saturday January 09, 2016 @01:14AM (#10870309)
You don't know how hardware works...
Re:How to C in 2016 by lmm on Friday January 08, 2016 @07:50AM (#10864486)
> I've read that if one uses int64_t with a compiler that targets a 32 bit system, doing various operations on the int64_t variable will be much slower because more machine code instructions need to be generated.

Yep, but presumably you picked int64_t for a reason. If you want a 32-bit variable, use int32_t.

It would be an extremely rare platform on which int16_t is noticeably slower than int. The very minor platform-specific performance improvement you might get from using int rather than int16_t is very rarely worth the risk of introducing an overflow on the few platforms where int is 16 bits[1], and not noticing it because int is longer than that on the platform you develop on.

[1] remember that a signed integer overflow is undefined behaviour which should be regarded as equivalent to a security flaw - the compiler is permitted to send all data your program has access to to the mafia if your program contains a signed integer overflow anywhere, and some compilers will

Re:How to C in 2016 by Triave on Friday January 08, 2016 @07:58AM (#10864511)
"int" doesn't reveal the intention of the programmer - they might just have used it because it's "int", not because it changes size based on what system you are targeting.

If they used 'int_fast16_t' from <stdint.h>, you have at least an idea that they considered that it might only be 16 bits, and that it can be a larger type on systems where operations on 64-bit types are faster.

Re:How to C in 2016 by caf on Friday January 08, 2016 @08:05AM (#10864549)
You are right, and this advice is wrong.

`int` is at least 16 bits, `long` is at least 32 bits, `long long` is at least 64 bits, and `size_t` is big enough to hold the size of the largest object you can allocate - and in most cases that is all you need to know.

The exact width types can be useful if you're marshalling data to and from an exact-width format, like a network protocol or file format.

Re:How to C in 2016 by Nursie on Friday January 08, 2016 @08:49AM (#10864758)
I disagree.

You should know the range of values that you are expecting to be holding in an integer, and choose the type accordingly.

Re:How to C in 2016 by caf on Friday January 08, 2016 @05:43PM (#10868872)
That does not contradict what I said. When choosing the type, all you need is a type that has at least the range you require, and the basic types give guaranteed minimum ranges.

The only advantage to choosing an exactly-N-bit type over an at-least-N-bit type is possibly saving some space in very large allocations. In most cases this won't be important - and when it is, the mininmum-width types from stdint.h (eg int_least32_t) are more apropos than the exact-width types.

The disadvantage is the impedance mis-match with all the existing libraries of code using the basic types.

Re:How to C in 2016 by CrLf on Friday January 08, 2016 @08:16AM (#10864602)
Don't agree with that either.

IMHO, "int" is perfectly acceptable if you just want to use the platform's preferred word size (and are fine with the possibility of it being only 16bit). Also, "char" is perfectly acceptable too if you're dealing with ASCII. This is valid for both the signed and unsigned variants.

The general rule should be using the language types as long as you're not assuming something about their size, only then use "stdint.h" because it makes your assumption more explicit.

Now, nobody uses "short" or "long" to mean "something smaller or shorter than the platform's int", so these are usually something to avoid.

Re:How to C in 2016 by AndyKelley on Friday January 08, 2016 @08:36AM (#10864697)
Is "the platform's preferred word size" meaningful? On x86_64, pointers are 8 bytes, but it's still faster to use smaller registers if you don't need the extra bits. For this system, at least, the correct type is the smallest one that is big enough to hold the maximum value your integer could be.
Re:How to C in 2016 by mpu on Friday January 08, 2016 @09:08AM (#10864885)
Bullshit, on x64 all 32 or 64 bit base arithmetic operations have the exact same cost, except the 64 bit version is one byte longer because of a REX prefix. This is why 32 bits is the natural int size for x64.
Re:How to C in 2016 by vardump on Saturday January 09, 2016 @02:25AM (#10870439)
Well, at least division isn't. On reasonable recent Intel Broadwell, signed 64-bit division (IDIV) takes 59 cycles, while signed 32-bit division takes just 9 cycles.

Granted, divisions are rare, but that's still nearly an order of magnitude difference in that corner case.

32-bit and 64-bit addition, multiplication, comparison, logical operations etc. are in almost all cases just as fast.

You'll still need more storage on the stack for 64-bit variables. So more cache misses (and perhaps TLB misses and page faults) when corresponding boundary is crossed.

Re:How to C in 2016 by amadvance on Friday January 08, 2016 @09:56AM (#10865231)
Being smaller usually results also in being faster for the cache effect.
Re:How to C in 2016 by xcgvgh on Friday January 08, 2016 @08:28AM (#10864645)
I use long when I need an int that can always represent ranges in -2^31-1 to 2^31-1, no assumptions necessary.

Same goes for unsigned versions and long long.

Re:How to C in 2016 by lmm on Friday January 08, 2016 @09:38AM (#10865106)
So int32_t, but making overflow bugs intermittent and architecture-dependent? Why would you want that?