3

I can do var b = *myObject; in the immediate window and it gives me a 0x123456 formatted value. But I can't do this in code. It then says

The * or -> operator must be applied to a pointer

Why can I do this in the immediate window, but not in code?

Ian
  • 30,182
  • 19
  • 69
  • 107
Mike de Klerk
  • 11,906
  • 8
  • 54
  • 76
  • 2
    I think this may have something to do with pointers to managed types not being allowed, only value types. At compile time it recognizes `*myObject` as a pointer to a managed type. In the immediate window it recognizes it as the address of the reference to `myObject` which is type `object&`, so is de-referencing that . I'm not sure about this, but just throwing that out there in the hope it might point (no pun intended) you to a better answer. – Stainy Sep 11 '15 at 11:22

1 Answers1

0

Example access with unsafe pointer v.s. marshal-ing

IntPtr pLineBuf;
int[] lineData;
//.
//.
//.

pLineBuf = Marshal.AllocHGlobal(8192 * sizeof(byte));
lineData = new int[8192];

// access elements like this
unsafe
{
    byte* pa = (byte*)plineBuf;  // cast from IntPtr
    for (int j=0; j<10; j++)
       lineData[j] = *(pa + j);    
}

// OR

for (int j=0; j<10; j++)
     lineData[j] = (Marshal.ReadByte(plineBuf + j));