tags:

views:

268

answers:

4

Why might using a "long long" in C or C++ be a bad thing?

I was compiling a runtime library the other day and in the code it checks to see if longs are 64bits and if not it uses a long long. But along with that, it sends out a #warning "using long long". I can't think of any reason for the "long long" being a warning unless it was leftover debug cruft from the developer.

Thanks Chenz

+8  A: 

Two reasons:

You don't know how long (haha!) a long long is, so if the code assumes that it is exactly 64 bits, there could be a problem.

If you use an old C compiler, it might not support long long.

Thomas Padron-McCarthy
I don't really see your first point as this is no different for any of the other generic integer types; C99 provides exact-width and domain-specific integer types for a reason...
Christoph
for the record: exact-width integers are *optional* in C99.
sellibitze
@Christoph: True. On the other hand, I don't know of any implementations where long is bigger than 64 bits, and it is probably _likelier_ that long is exactly 64 bits, if it isn't shorter, than that long long is.
Thomas Padron-McCarthy
@sellibitze: according to C99 7.18.1.1, §3 types of exact widths 8, 16, 32, and 64 have to be provided if such types are available on the architecture in question; if not, `long long` also can't be 64bit and the whole point is moot
Christoph
"...if such types are available..." --> a conforming implementation doesn't have to have these types --> optional
sellibitze
+4  A: 

I agree with Thomas Padron-McCarth. It is much better to check for the existence of int64_t (signed) or uint64_t (unsigned) respectively, and use them if they exist. Its the same sort of sanity that led to size_t.

Tim Post
+9  A: 

As far as I know, long long is currently standard only in C99. It will also be a type in C++0x, but most modern compilers should support it already.

However, for fixed-sized integers one might use the C99 header <stdint.h>, or in C++ <boost/cstdint.hpp>

UncleBens
+3  A: 

It isn't portable. Specifically, the windows compiler didn't use to support long long, and you had to use __int64 and unsigned __int64 instead (but couldn't use necessarily use on non-windows platforms).

This was a while ago, so perhaps now there is a better chance of always having uint64_t and int64_t available.

Peeter Joot