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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 11:53:28  来源:igfitidea点击:

sizeof(int) on x64?

c#64-bitclrsizeof

提问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 unsafecontexts or as a IntPtrvalue which cannot be used for pointer-arithmetic). Contrast this with ILP64 where ints are also 64-bits.

多种 64 位数据模型;Microsoft为 .NET使用LP64longs 和指针都是 64 位(尽管 C 风格的指针只能在 C# 的unsafe上下文中使用,或者作为 IntPtr不能用于指针算术的值)。将此与 ILP64 进行对比,其中int也是 64 位。

Thus, on all platforms, intis 32-bits and longis 64-bits; you can see this in the names of the underlying types System.Int32and System.Int64.

因此,在所有平台上,int都是 32 位和long64 位;您可以在基础类型的名称System.Int32System.Int64.

回答by Andrew Hare

The keyword intaliases System.Int32which still requires 4 bytes, even on a 64-bit machine.

即使在 64 位机器上,仍然需要 4 个字节的关键字int别名System.Int32

回答by Ben S

intmeans Int32in .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.

这是C#中所有类型表,以及它们映射到 .NET 明智的内容。

回答by Brian Rasmussen

Remember intis just a compiler alias for the basic type Int32. Given that it should be obvious why intis only 32 bits on a 64 bit platform.

记住int只是基本类型的编译器别名Int32。鉴于很明显为什么int在 64 位平台上只有 32 位。

回答by Brian R. Bondy

You may be thinking of an intpointer 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 intis still 4 bytes whether you are on an x86 or an x64. That is to say that an intwill always correspond to System.Int32.

int无论您使用的是 x86 还是 x64,的含义仍然是 4 个字节。也就是说, anint总是对应于System.Int32

回答by andleer

An Int32is 4 bytes on x86 and x64. An Int64is 8 bytes either case. The C# inttype 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
    }

回答by Shafeeq

int i;
int size = BitConverter.GetBytes(i).GetLength(0);

Fiddle Sample

小提琴样本