tags:

views:

277

answers:

2
  • It's not the largest integer type anymore now that there's "long long".
  • It's not a fixed-width type: It's 32 bits on some platforms and 64 on others.
  • It's not necessarily the same size as a pointer (for example, on 64-bit Windows)

So, does "long" have any meaning anymore? Is there ever a reason to declare a long instead of a ptrdiff_t or int64_t?

+16  A: 

Is there ever a reason to declare a long instead of a ptrdiff_t or int64_t?

There never was in those cases. If you want a pointer difference, or a specifically 64-bit value, you should be using ptrdiff_t or int64_t. You should never have been using long in the first place, except maybe behind a platform-dependent typedef.

You should use long when you need at least a 32-bit integer, because int is only guaranteed to be at least 16 bits. If you need a native type, but need at least 32-bits, use long. If the 16-bit limitation is acceptable on some old platforms (that probably don't matter and your code probably won't be compiled on ever) then it doesn't particularly matter.

Chris Lutz
+1 for the at least a certain size integer idea.
Ninefingers
I should note that that requirement can also be satisfied by C99's `int_leastX_t` types, but less reliably since few compilers support C99.
Chris Lutz
+4  A: 

There's a subtle difference between still useful and something you should habitually use. The long type still thrives, as Chris Lutz noted behind many system and platform specific types (though, usually as unsigned).

When you know the data you will be working with is always going to fit in that type (agreeably, better if you also know the signedness), there is no particular reason not to use it, especially if you have limited space in a structure to work with.

However, in most cases, for the sake of future maintainers, its much better to use intxx_t or uintxx_t wherever possible. For instance, you don't know that a UNIX epoch date will always fit inside an unsigned long on 32 bit platforms (hence, time_t), unless you have it on good authority that the world will end before it rolls over :)

Tim Post
Alright, cue the 2012 jokes ...
Tim Post
I would argue that unless you have a specific reason to need *exactly* XX bits, you shouldn't use `intXX_t`. Usually you really need *at least* XX bits, in which case you can simply use `int` for X <= 16; `long` for 16 < X <= 32; and `long long` for 32 < X <= 64. In the UNIX epoch case, you should be using `time_t`. (The most common reason for needing *exactly* XX bits is dealing with an on-disk or on-the-wire format).
caf
@caf: Impossible to argue with that. However, a lot of the code that I write is written with the idea that a certain member will never 'be any bigger than' a certain type, when in fact everything starts out much smaller than I've actually planned for. Often the case when you have to hold query results, tabulate sets of stuff that other things crunched, etc. I think I'm more defensive than efficient, but I kind of have no choice most of the time :)
Tim Post