sizeof(int) on x64?

前端 未结 7 1535
眼角桃花
眼角桃花 2020-11-28 13:31

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 r

相关标签:
7条回答
  • 2020-11-28 13:38

    There are various 64-bit data models; Microsoft uses LP64 for .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.

    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.

    0 讨论(0)
  • 2020-11-28 13:40

    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:

        unsafe
        {
            var size = sizeof(IntPtr); // 4 on x86 bit machines. 8 on x64
        }
    
    0 讨论(0)
  • 2020-11-28 13:47

    The keyword int aliases System.Int32 which still requires 4 bytes, even on a 64-bit machine.

    0 讨论(0)
  • 2020-11-28 13:50
    int i;
    int size = BitConverter.GetBytes(i).GetLength(0);
    

    Fiddle Sample

    0 讨论(0)
  • 2020-11-28 13:54

    int means Int32 in .NET languages. This was done for compatibility between 32- and 64-bit architectures.

    Here's the table of all the types in C# and what they map to .NET wise.

    0 讨论(0)
  • 2020-11-28 13:55

    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.

    0 讨论(0)
提交回复
热议问题