What is the "pinned" keyword and how do I use it in MS VC# Express?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ChaosMageX
    New Member
    • Jun 2009
    • 1

    What is the "pinned" keyword and how do I use it in MS VC# Express?

    I copied some code from a Mono 2.4 library to use in my own application.
    I'm getting a problem because one of the variable declarations involves the keyword "pinned" between the identifier and the instance name.
    Code:
    public override unsafe void PutBytes(byte[] dest, int destIdx, double value)
    {
        base.Check(dest, destIdx, 8);
        byte* pinned numPtr = $(dest[destIdx]);
        long* numPtr2 = (long*) &value;
        *((long*) numPtr) = numPtr2[0];
        numPtr = null;
    }
    I've never heard of this keyword before. From what I've read on Googled websites, "pinning" has to with the Garbage Collector not moving its memory location or something like that.
    Unfortunately, MS Visual C# Express and its compiler have also never heard of this keyword, since they're shouting about the following errors having to do with that line:
    ; expected
    The name 'numPtr' does not exist in the current context

    I tried changing the culprit line's syntax to this:
    Code:
        fixed(byte* numPtr = $(dest[destIdx]));
    Unfortunately, MS VC# Express still gives me the error "The name 'numPtr' does not exist in the current context" for the other two lines where it is used.

    What is the pinned keyword, and how do I get MS Visual C# 2008 Express Edition to recognize it?

    EDIT:
    I know there has to be a working equivalent in MS VC# Express, because this is what the code is supposed to look like in MSIL:
    Code:
        .method public hidebysig virtual instance void 
                PutBytes(uint8[] dest,
                         int32 destIdx,
                         float32 'value') cil managed
        {
          // Code size       28 (0x1c)
          .maxstack  6
          .locals init (uint8* pinned V_0,
                   uint32* V_1)
          IL_0000:  ldarg.0
          IL_0001:  ldarg.1
          IL_0002:  ldarg.2
          IL_0003:  ldc.i4.4
          IL_0004:  call       instance void Mono.DataConverter::Check(uint8[],
                                                                       int32,
                                                                       int32)
          IL_0009:  ldarg.1
          IL_000a:  ldarg.2
          IL_000b:  ldelema    System.Byte
          IL_0010:  stloc.0
          IL_0011:  ldarga.s   'value'
          IL_0013:  stloc.1
          IL_0014:  ldloc.0
          IL_0015:  ldloc.1
          IL_0016:  ldind.u4
          IL_0017:  stind.i4
          IL_0018:  ldc.i4.0
          IL_0019:  conv.u
          IL_001a:  stloc.0
          IL_001b:  ret
        } // end of method CopyConverter::PutBytes
    So, what is the equivalent to the pinned keyword in MS VC# 2008 Express Edition?
  • IanWright
    New Member
    • Jan 2008
    • 179

    #2
    Honestly I have no idea. I would recommend you try some Mono forums

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      That fixed block is an unsafe context, right? If you do want to go the unsafe context route then just allocate the memory using stackalloc. Storing the value on the stack means that it won't be moved after garbage collection as gc only works on the heap.
      P.S I would rather simply try and understand what the method was trying to achieve and implement it in a safe way. Direct conversion between languages or variants thereof is never a good idea.

      Comment

      Working...