C# x64 上的 sizeof(int)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/651956/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
sizeof(int) on x64?
提问by iheartcsharp
When I do sizeof(int)
in my C#.NET project I get a return value of 4. I set the project type to x64, so why does it say 4 instead of 8? Is this because I'm running managed code?
当我sizeof(int)
在我的 C#.NET 项目中执行时,我得到的返回值为 4。我将项目类型设置为 x64,那么为什么它说的是 4 而不是 8?这是因为我正在运行托管代码吗?
采纳答案by Dаn
There are various 64-bit data models; Microsoft uses LP64for .NET: both longs and pointers are 64-bits (although C-style pointers can only be used in C# in unsafe
contexts or as a IntPtr
value which cannot be used for pointer-arithmetic). Contrast this with ILP64 where ints are also 64-bits.
有多种 64 位数据模型;Microsoft为 .NET使用LP64:longs 和指针都是 64 位(尽管 C 风格的指针只能在 C# 的unsafe
上下文中使用,或者作为 IntPtr
不能用于指针算术的值)。将此与 ILP64 进行对比,其中int也是 64 位。
Thus, on all platforms, int
is 32-bits and long
is 64-bits; you can see this in the names of the underlying types System.Int32
and System.Int64
.
因此,在所有平台上,int
都是 32 位和long
64 位;您可以在基础类型的名称System.Int32
和System.Int64
.
回答by Andrew Hare
The keyword int
aliases System.Int32
which still requires 4 bytes, even on a 64-bit machine.
即使在 64 位机器上,仍然需要 4 个字节的关键字int
别名System.Int32
。
回答by Ben S
int
means Int32
in .NET languages. This was done for compatibility between 32- and 64-bit architectures.
int
手段Int32
在.NET语言。这样做是为了 32 位和 64 位体系结构之间的兼容性。
Here's the table of all the typesin C# and what they map to .NET wise.
回答by Brian Rasmussen
Remember int
is just a compiler alias for the basic type Int32
. Given that it should be obvious why int
is only 32 bits on a 64 bit platform.
记住int
只是基本类型的编译器别名Int32
。鉴于很明显为什么int
在 64 位平台上只有 32 位。
回答by Brian R. Bondy
You may be thinking of an int
pointer or System.IntPtr
. This would be 8 bytes on an x64 and 4 bytes on an x86. The size of a pointer shows that you have 64-bit addresses for your memory. (System.IntPtr.Size
== 8 on x64)
您可能会想到int
指针或System.IntPtr
. 这在 x64 上为 8 个字节,在 x86 上为 4 个字节。指针的大小表明您的内存有 64 位地址。(System.IntPtr.Size
在 x64 上 == 8)
The meaning of int
is still 4 bytes whether you are on an x86 or an x64. That is to say that an int
will always correspond to System.Int32
.
int
无论您使用的是 x86 还是 x64,的含义仍然是 4 个字节。也就是说, anint
总是对应于System.Int32
。
回答by andleer
An Int32
is 4 bytes on x86 and x64. An Int64
is 8 bytes either case. The C# int
type is just an alias for System.Int32
. Same under both runtime environments. The only type that does change depending on the runtime environment is an IntPtr
:
AnInt32
在 x86 和 x64 上是 4 个字节。AnInt64
是 8 个字节。C#int
类型只是System.Int32
. 在两种运行时环境下相同。唯一会根据运行时环境发生变化的类型是IntPtr
:
unsafe
{
var size = sizeof(IntPtr); // 4 on x86 bit machines. 8 on x64
}