开发者

Does the size of "int" change on x64 built applications? [duplicate]

开发者 https://www.devze.com 2023-02-17 13:49 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicate: sizeof(int) on x64? The size of IntPtr changes from 4 to 8 when compiled for x64 versus x86.
This question already has answers here: Closed 11 years ago.

Possible Duplicate:

sizeof(int) on x64?

The size of IntPtr changes from 4 to 8 when compiled for x64 versus x86.

Does the size of int also change, or is it still Int32?

This question is particularly important when using 开发者_如何学Pythonpinvoke and dealing with interop calls. Do all "int" types need to be explicitly changed to be declared as Int32?


No; int is always synonymous with Int32.

For P/Invoke, you need to use IntPtr to vary with bitness.


No. "int" in C# is always defined as an alias for Int32 as part of the C# language specification, which is always the same size. Using "int" or "Int32" will produce exactly the same IL.


It is still an Int32 in C#. Unmanaged C++ though, int will become a 64bit number.


Data types in Windows

Every application and every operating system has an abstract data model. Many applications do not explicitly expose this data model, but the model guides the way in which the application's code is written. In the 32-bit programming model (known as the ILP32 model), integer, long, and pointer data types are 32 bits in length. Most developers have used this model without realizing it.

In 64-bit Microsoft Windows, this assumption of parity in data type sizes is invalid. Making all data types 64 bits in length would waste space, because most applications do not need the increased size. However, applications do need pointers to 64-bit data, and they need the ability to have 64-bit data types in selected cases. These considerations led the Windows team to select an abstract data model called LLP64 (or P64). In the LLP64 data model, only pointers expand to 64 bits; all other basic data types (integer and long) remain 32 bits in length.

The .NET CLR for 64-bit platforms uses the same LLP64 abstract data model. In .NET there is an integral data type, not widely known, that is specifically designated to hold 'pointer' information: IntPtr whose size is dependent on the platform (e.g., 32-bit or 64-bit) it is running on. Consider the following code snippet:

public void SizeOfIntPtr() {
Console.WriteLine( "SizeOf IntPtr is: {0}", IntPtr.Size );
}

When run on a 32-bit platform you will get the following output on the console:

CopySizeOf IntPtr is: 4

On a 64-bit platform you will get the following output on the console:

CopySizeOf IntPtr is: 8

If you want to check at runtime whether or not you are running in a 64-bit environment, you can use the IntPtr.Size as one way to make this determination.


No, x64 refers to the size of pointers and not to individual pieces of data like an int.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号