When initializing the values in an array of long
values, I noticed that the sizes were 4 bytes and not 8 bytes in the values.
sizeof(unsigned long)
shows as 4 bytes:
The same value is shown in both 32-bit and 64-bit versions.
Version:
Microsoft Visual Studio Community 2022 (64-bit) - Current
Version 17.12.4
When initializing the values in an array of long
values, I noticed that the sizes were 4 bytes and not 8 bytes in the values.
sizeof(unsigned long)
shows as 4 bytes:
The same value is shown in both 32-bit and 64-bit versions.
Version:
Microsoft Visual Studio Community 2022 (64-bit) - Current
Version 17.12.4
3 Answers
Reset to default 3This seems correct, following the table found on the Data Type Ranges Microsoft documentation page.
unsigned long
(same type as unsigned long int
) is 4 bytes, while unsigned long long
(same type as unsigned __int64
is 8 bytes.
C++ doesn’t specify the exact size of a long
. It doesn’t specify the exact size of other integer types like int
either. It only gives you minimum sizes. An int
is guaranteed to be at least 2 bytes (but it can be larger) while a long is guaranteed to be at least 4 bytes (but it can be larger). The exact sizes are platform defined. So as you can see MSVC on Windows defines a long
to be 4 bytes, which is enough to conform to the C++ standard, while GCC and Clang on Linux define it to be 8 bytes.
This is why C++ also defines fixed-width types in the cinttypes
and cstdint
headers, to make it easier to write portable code that works the same on every platform. If you really want an 8-byte integer type then you can use std::int64_t
(or its unsigned version `std::uint64_t) and it is guaranteed to be exactly 8 bytes (64 bits). Here’s a list of all fixed-width integer types:
https://en.cppreference/w/cpp/types/integer
The reason int_t were introduced (as optional types) is the standard allows flexibility in the size of other integer types in order to allow easy porting C & C++ compilers to computers with different word sizes. My first job was on a distributed system, and one of the computers had 36 bits word.
To quote The C Programming Language ANSI C version
The intent is that short and long should provide different lengths of integers where practical; int will normally be the natural size for a particular machine. short is often 16 bits long, and int either 16 or 32 bits. Each compiler is free to choose appropriate sizes for its own hardware, subject only to the the restriction that shorts and ints are at least 16 bits, longs are at least 32 bits, and short is no longer than int, which is no longer than long.
The c99 standard (same for C11 and C18), paragraph 5.2.4.2.1 Sizes of integer types <limits.h> says
The values given below shall be replaced by constant expressions suitable for use in #if preprocessing directives. Moreover, except for CHAR_BIT and MB_LEN_MAX, the following shall be replaced by expressions that have the same type as would an expression that is an object of the corresponding type converted according to the integer promotions. Their implementation-defined values shall be equal or greater in magnitude (absolute value) to those shown, with the same sign. maximum value for an object of type unsigned long int ULONG_MAX 4294967295 // 2^32 - 1
Unsigned long has to be at least 32 bits. So MS's choice of 4 x 8 bit bytes is valid.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745283314a4620408.html
long long
is a GCC thing.,[unsigned] __int64
is the Microsoft datatype. Note thatsize_t
andptrdiff_t
are 64 bit when targeting Win64, they are by definition pointer sized. The former is unsigned, the latter is signed. – Seva Alekseyev Commented Jan 30 at 2:26uint64_t
it will expand to whatever works for the current platform, don't rely on C types likelong
unless you are interfacing with a C API in which case all compilers on the system will agree on its size, but it changes for each platform – Ahmed AEK Commented Jan 30 at 8:00long long
is a thing in both the C standard and the C++ standard. There's nothing GCC-specific to it. – MSalters Commented Jan 30 at 12:46