Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unsafe pointer/array notation in C#

Tags:

c#

pointers

Suppose I have:

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

What is something? The value of the memory address 4 bytes down from start?

like image 221
xofz Avatar asked Feb 22 '26 20:02

xofz


1 Answers

Say start points to memory location 0x12345678, and memory looks like this:

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

then something equals 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.

like image 113
RichieHindle Avatar answered Feb 24 '26 13:02

RichieHindle