By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,917 Members | 1,313 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,917 IT Pros & Developers. It's quick & easy.

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

P: 1
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.
Expand|Select|Wrap|Line Numbers
  1. public override unsafe void PutBytes(byte[] dest, int destIdx, double value)
  2. {
  3.     base.Check(dest, destIdx, 8);
  4.     byte* pinned numPtr = $(dest[destIdx]);
  5.     long* numPtr2 = (long*) &value;
  6.     *((long*) numPtr) = numPtr2[0];
  7.     numPtr = null;
  8. }
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:
Expand|Select|Wrap|Line Numbers
  1.     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:
Expand|Select|Wrap|Line Numbers
  1.     .method public hidebysig virtual instance void 
  2.             PutBytes(uint8[] dest,
  3.                      int32 destIdx,
  4.                      float32 'value') cil managed
  5.     {
  6.       // Code size       28 (0x1c)
  7.       .maxstack  6
  8.       .locals init (uint8* pinned V_0,
  9.                uint32* V_1)
  10.       IL_0000:  ldarg.0
  11.       IL_0001:  ldarg.1
  12.       IL_0002:  ldarg.2
  13.       IL_0003:  ldc.i4.4
  14.       IL_0004:  call       instance void Mono.DataConverter::Check(uint8[],
  15.                                                                    int32,
  16.                                                                    int32)
  17.       IL_0009:  ldarg.1
  18.       IL_000a:  ldarg.2
  19.       IL_000b:  ldelema    System.Byte
  20.       IL_0010:  stloc.0
  21.       IL_0011:  ldarga.s   'value'
  22.       IL_0013:  stloc.1
  23.       IL_0014:  ldloc.0
  24.       IL_0015:  ldloc.1
  25.       IL_0016:  ldind.u4
  26.       IL_0017:  stind.i4
  27.       IL_0018:  ldc.i4.0
  28.       IL_0019:  conv.u
  29.       IL_001a:  stloc.0
  30.       IL_001b:  ret
  31.     } // end of method CopyConverter::PutBytes
So, what is the equivalent to the pinned keyword in MS VC# 2008 Express Edition?
Jun 22 '09 #1
Share this Question
Share on Google+
2 Replies


100+
P: 179
Honestly I have no idea. I would recommend you try some Mono forums
Jun 25 '09 #2

10K+
P: 13,264
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.
Jun 25 '09 #3

Post your reply

Sign in to post your reply or Sign up for a free account.