INT16 - 字节容量in.net?字节、容量、in、net

2023-09-04 01:19:09 作者:一腔孤勇

为什么:

short a=0;
Console.Write(Marshal.SizeOf(a));

节目 2

但是,如果我看到了IL code,我看到:

But if I see the IL code i see :

/*1*/   IL_0000:  ldc.i4.0    
/*2*/   IL_0001:  stloc.0     
/*3*/   IL_0002:  ldloc.0     
/*4*/   IL_0003:  box         System.Int16
/*5*/   IL_0008:  call        System.Runtime.InteropServices.Marshal.SizeOf
/*6*/   IL_000D:  call        System.Console.Write

最不发达国家的行#1指示:

The LDC at line #1 indicates :

0推到堆栈中为 INT32

所以必须是 4 字节占用。

整型尺寸节目 2 字节...

我在想什么吗?有多少字节没有短期实际采取纪念品?

What am I missing here ? how many byte does short actually take in mem?

我听说过一个情况,那里是一个填充,以4个字节,因此会更快处理。是它的情况也在这里?

I've heard about a situations where there is a padding to 4 bytes so it would be faster to deal with. is it the case also here?

(请忽略SyncRoot上和GC根标志字节我只是询问2对4)的

推荐答案

这是pretty的容易分辨什么是采取一看的最不发达国家提供说明。注意一组可用的操作类型的限制,有否版本可用的负载类型短的恒定。刚刚的int,long,float和double。这些限制是在其他地方看到,欧普codes.Add指令,例如类似限制,较小的类型之一添加变量不支持。

It is pretty easy to tell what's going on by taking a look at the available LDC instructions. Note the limited set of operand types available, there is no version available that load a constant of type short. Just int, long, float and double. These limitations are visible elsewhere, the Opcodes.Add instruction for example is similarly limited, no support for adding variables of one of the smaller types.

在IL指令集是非常有意这样设计的,它反映了一个简单的32位处理器的能力。那种处理器想到的是RISC类型的,他们有自己的干草日在nineteens。很多32位CPU寄存器只能处理32位的整数和IEEE-754浮点类型。英特尔的x86核心不是一个很好的例子,虽然很常用的,它是真正支持加载和做算术的8位和16位运算CISC架构设计。但是,这更多的是历史偶然的,它使程序容易上的8位8080和16位8086处理器开始的机械翻译。但是,这种能力不来的自由,操纵16位值实际花费额外的CPU周期。

The IL instruction set was very much designed intentionally this way, it reflects the capabilities of a simple 32-bit processor. The kind of processor to think of is the RISC kind, they had their hay-day in the nineteens. Lots of 32-bit cpu registers that can only manipulate 32-bit integers and IEEE-754 floating point types. The Intel x86 core is not a good example, while very commonly used, it is a CISC design that actually supports loading and doing arithmetic on 8-bit and 16-bit operands. But that's more of a historical accident, it made mechanical translation of programs easy that started on the 8-bit 8080 and 16-bit 8086 processors. But such capability doesn't come for free, manipulating 16-bit values actually costs an extra cpu cycle.

制作IL与32位处理器性能的良好匹配显然使得实施的抖动更简单的家伙的工作。存储单元仍然可以是更小的尺寸,但只加载,存储和转换需要得到支持。而仅在需要时,您的一个变量是一个局部变量,一个占据堆栈帧或CPU寄存器的32位反正。只需要存储到内存中被截断到合适的大小。

Making IL a good match with 32-bit processor capabilities clearly makes the job of the guy implementing a jitter much simpler. Storage locations can still be a smaller size, but only loads, stores and conversions need to be supported. And only when needed, your 'a' variable is a local variable, one that occupies 32-bits on the stack frame or cpu register anyway. Only stores to memory need to be truncated to the right size.

目前,否则在code段毫不含糊。变量值需要进行装箱,因为Marshal.SizeOf()采用类型的对象的参数的。装箱值标识值的类型手柄类型,它会指向System.Int16。 Marshal.SizeOf()有内置的知识,知道它需要2个字节。

There is otherwise no ambiguity in the code snippet. The variable value needs to be boxed because Marshal.SizeOf() takes an argument of type object. The boxed value identifies the type of value by the type handle, it will point to System.Int16. Marshal.SizeOf() has the built-in knowledge to know it takes 2 bytes.

这些限制并反映在C#语言,并导致不一致。这种编译错误永远befuddles和惹恼C#程序员:

These restrictions do reflect on the C# language and cause inconsistency. This kind of compile error forever befuddles and annoys C# programmers:

    byte b1 = 127;
    b1 += 1;            // no error
    b1 = b1 + 1;        // error CS0266

这是白细胞介素限制,因此,不存在添加操作员需要字节操作数。它们需要转换到下一个较大的兼容型,电磁 INT 的在这种情况下。所以它适用于一个32位的RISC处理器。现在有一个问题,32位的 INT 的结果需要被敲定回可以存储只有8位的变量。 C#语言应用于锤子本身在第一任务,但不合逻辑需要投锤子在第二分配。

Which is a result of the IL restrictions, there is no add operator that takes byte operands. They need to be converted to the next larger compatible type, int in this case. So it works on a 32-bit RISC processor. Now there's a problem, the 32-bit int result needs to be hammered back into a variable that can store only 8-bits. The C# language applies that hammer itself in the 1st assignment but illogically requires a cast hammer in the 2nd assignment.