From the Difference Between “long long” and “long int”: Portability and Suggestions

Overview

The keyword long is just a type modifier which indicate the length of the real type integer, the portability and good practices about the difference is given.

Suggestions first: long in C++ and portability

I better give the suggestions first followed by concrete supporting maters.

  1. long is not a type, but just a type modifier just like short/signed/unsigned, think about it, integer is the type, can you image a type called long? no! There is only type of integer, and the long modifier indicate the length of bytes used in memory for integer.
  2. for portability we need to choose int32_t or similar type declarations, which is fixed size, the size of other type specifier like long would vary on different platforms. It is only because usually, we do not depend on the actually size, and the common platform support 4 bytes int, we do not care about it too much, only those who need to make the code really portable on quite many different platforms need to be concerned about it.
  3. The size of C++ integer types is implementation specific
  4. The business logic cannot assume and depend on the size of int say to be a particular size say 4
  5. If you are not concerned about the memory at all, I suggest make the type as big as possible to use long long for the integers.

long is just a modifier: ”long long” vs “long int”

As simply using long to declare the integer type variable in C++ so frequently, many people would treat it as a type in C++, but the fact is that it is not, this is important to keep in mind for better practices in your C++ program. The following summary from stackoverflow is quite motivating:

There are several shorthands for built-in types.

  • short is (signedshort int
  • long is (signedlong int
  • long long is (signedlong long int.

On many systems, short is 16-bit, long is 32-bit and long long is 64-bit. However, keep in mind that the standard only requires

sizeof(char) == 1
sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long) <= sizeof(long long)

And a consequence of this is that on an exotic system, sizeof(long long) == 1 is possible.

What Intel says about “long”

The following is from Intel: “size of long integer type on different architecture and OS

The size of a “long” integer varies between architectures and operating systems.

The Intel Compiler is compatible and inter-operable with Microsoft* Visual C++ on Windows* and with gcc* on Linux* and Mac OS X*. Consequently, the sizes of fundamental types are the same as for these compilers. The size of  a long integer in particular depends on the operating system and the targeted architecture as follows:

OS           arch           size
Windows       IA-32        4 bytes
Windows       Intel 64     4 bytes
Windows       IA-64        4 bytes
Linux            IA-32        4 bytes
Linux            Intel 64     8 bytes
Linux            IA-64        8 bytes
Mac OS X      IA-32        4 bytes
Mac OS X      Intel 64     8 bytes

Consequently, when some programs that use “long” are ported from IA-32 Linux to Intel 64 Linux, or from Intel 64 Windows to Intel 64 Linux, the behavior might change. (Note that the size of pointers is expected to change between IA-32 and Intel 64).

      In addition, the size of a “long double” also varies by operating system.

On Windows, the size is 8 bytes by default. On Linux or Mac OS X, 10 bytes are used for the representation of a long double, although the allocated size may be larger (12 bytes on IA-32 Linux; 16 bytes elsewhere).

Suggestion:   If it is important to you for integer types to have the same size on all Intel platforms, then consider replacing “long” by either “int”  or “long long”. The size of the “int” integer type is 4 bytes and the size of the “long long” integer type is 8 bytes for all the above combinations of operating system and architecture.

On Windows, the representation of “long double” may be increased to 10 bytes by use of the command line switch /Qlong-double. The corresponding memory allocation is 16 bytes.

Summary

The keyword long is just a type modifier which indicate the length of the real type integer, the portability and good practices about the difference is given.

Written on December 10, 2014