简体   繁体   English

C#中的不安全指针/数组表示法

[英]Unsafe pointer/array notation in C#

Suppose I have: 假设我有:

unsafe {
    byte* start = GetStartLocation();
    int something = start[4];
}

What is something ? 什么something The value of the memory address 4 bytes down from start? 内存地址的值从开始减少4个字节?

Say start points to memory location 0x12345678, and memory looks like this: start指向内存位置0x12345678,内存如下所示:

  0x12345678   0x12
  0x12345679   0x34
  0x1234567a   0x56
  0x1234567b   0x78
  0x1234567c   0x9a
  0x1234567d   0xbc

then something equals 0x9a . 然后something等于0x9a

The fact that something has type int doesn't matter to how start[4] is interpreted - it gets the byte value of the byte 4 locations away from start . something事件类型为int的事实与start[4]的解释方式无关 - 它从字节4位置start获取byte值。

The value of something is the byte value of offset 4 from start widened to an int type. 某事物的值是从开始加宽到int类型的偏移量4的字节值。

It's equivalent to the following code 它等同于以下代码

byte temp = start[4];
int something = temp;

start[4]将评估为:

*(start + 4)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM