最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c++ - How come in Visual Studio 2022 the sizeof unsigned long is 4 bytes and not 8 bytes? - Stack Overflow

programmeradmin0浏览0评论

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

Share Improve this question edited Jan 30 at 17:41 Timothy G. 9,3337 gold badges45 silver badges65 bronze badges asked Jan 29 at 18:56 lsalamonlsalamon 8,1846 gold badges52 silver badges65 bronze badges 7
  • 1 On Windows, long is 32-bit. For 64-bit you want long long. To be explicit you can use LONGLONG, INT64, int64_t, etc. – Luke Commented Jan 29 at 19:24
  • 2 Why do you expect a size of 8 bytes? Please edit your question to add a reference (to the standard). – the busybee Commented Jan 29 at 19:32
  • long long is a GCC thing., [unsigned] __int64 is the Microsoft datatype. Note that size_t and ptrdiff_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:26
  • In c++ if you rely on the number of bytes then use a type that guarantees it like uint64_t it will expand to whatever works for the current platform, don't rely on C types like long 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:00
  • 3 @SevaAlekseyev: Nonsense. long 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
 |  Show 2 more comments

3 Answers 3

Reset to default 3

This 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.

发布评论

评论列表(0)

  1. 暂无评论