What is the size of the int and long types specified in the C++ standard?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
What is the size of the int and long types specified in the C++ standard?
I'm seeking for specific information on the sizes of basic C++ types. I understand that it is determined by the architecture (16 bits, 32 bits, or 64 bits) and the compiler.
But are there any C++ standards?
On a 32-bit architecture, I'm using Visual Studio 2008. This is what I get:
I looked for solid information on the sizes of char, short, int, long, double, float (and additional kinds I didn't think of) under different architectures and compilers, but had little luck and only got this [link moderated].
Last edited by astrogeek; 11-23-2022 at 01:07 PM.
Reason: spammish link moderated
The C and C++ standards don't define type sizes. Type sizes are compiler/architecture/OS/Data-model dependent. Take a look at this C data types and 64-bit_data_models
Now, for Visual C++, Microsoft has fixed sizes, probably to maximize compatibility between Windows versions. See Sizes of built-in types:
Most built-in types have implementation-defined sizes. The following table lists the amount of storage required for built-in types in Microsoft C++. In particular, long is 4 bytes even on 64-bit operating systems.
Code:
Type | Size
--------------------------------------------------------------------------
bool, char, char8_t, unsigned char, signed char, __int8 | 1 byte
char16_t, __int16, short, unsigned short, wchar_t, __wchar_t | 2 bytes
char32_t, float, __int32, int, unsigned int, long, unsigned long | 4 bytes
double, __int64, long double, long long, unsigned long long | 8 bytes
As mentioned before, just check the size of the type you are interested with:
And to complete @wainamoinen's thought, you should use these definitions if you actually care about the "size of" an integer and want your code to be portable. (This also makes your intentions clear to the human reader ...)
In the case of struct, you also need to be aware of the possibility of padding. Unless you specify that the struct is to be "packed" into the minimal amount of space – and I don't believe that there is a standard way to do this (unlike Pascal) – the compiler might insert extra bytes in order to align the various struct members most advantageously for the hardware in question. (For example, highly-pipelined 64-bit architectures "like" for things to sit on 64-bit address boundaries because the pipeline works better that way.) Unless you specify that it mustn't do this, the compiler presumes that it is free to do so.
Last edited by sundialsvcs; 11-23-2022 at 08:54 AM.
Optimizing the sizes of integer variables does not have much benefit on modern hardware.
I recommend using "long" by default (rather than "int") to preclude the possibility of overflow on 64-bit machines. In practice, "long" will never overflow on 64-bit machines.
The shorter types are useful only in specific circumstances:
* 32-bit int for pixel coordinates, RGBA values, and Unicode characters
* 16-bit short for audio data
* 8-bit char for strings
Probably the only situation where this would actually become important is when you need to match some externally-defined struct ... and you somehow don't have access to a public source-library that already does it for you, which is unlikely.
Vendor-defined type definitions such as "WPARAM" or "LPARAM" should always be followed when available, because their libraries make use of the same definitions, and they control those definitions. Above all, in your source code you should make your intentions crystal clear to the human reader: "say what you mean." (And if possible ... and this depends on the language you are using ... "have the compiler be able to catch your stpiud misteaks.") "Strong type-checking is a virtue."
Last edited by sundialsvcs; 11-23-2022 at 04:19 PM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.