473,386 Members | 1,798 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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.
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
2 2481
IanWright
179 100+
Honestly I have no idea. I would recommend you try some Mono forums
Jun 25 '09 #2
r035198x
13,262 8TB
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

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

Similar topics

2
by: Ronny Sigo | last post by:
Hello all, I already put the same question, only now I have more to tell ... Although I used this code before in the same routine (only the fieldname of the table differs) ___ at this point in the...
2
by: Mark Oliver | last post by:
Hi, I want to pass a reference to a char type to a class constructor, then read/write to the passed char later in my code. Are the below Asserts right? Thanks, Mark
3
by: Pablo Gutierrez | last post by:
I have a C# method that reads Binary data (BLOB type) from a database and returns the data an array of bytes (i.e byte outbyte = new byte;). The BLOB column is saved into the database by a C...
12
by: Olaf Baeyens | last post by:
I am porting some of my buffer class code for C++ to C#. This C++ class allocates a block of memory using m_pBuffer=new BYTE; But since the class is also used for pointers for funtions that uses...
2
by: coz | last post by:
I created a wrapper class for a dll written in C. Will the following code prevent the "ENTIRE ARRAY" from being moved, not just the first array element? The Wrapper class will be instantiated in...
7
by: Ioannis Vranos | last post by:
Consider the code: wchar_t __pin *p= &(someCommand->ToCharArray()); _wsystem(p); p=0;
17
by: Peter Oliphant | last post by:
In the 'old days', we could create a pointer to an instance of a variable like so: int i = 58 ; int* i_ptr = &i ; int j = *i_ptr ; // j = 58 Now, in /clr how do we do the same? That is,...
25
by: Koliber (js) | last post by:
sorry for my not perfect english i am really f&*ckin angry in this common pattern about dispose: ////////////////////////////////////////////////////////// Public class...
16
by: Atmapuri | last post by:
Hi! It would be great, if the pinned arrays would be garbage collectable. This would greatly reduce the amount of copy back and forth the managed and unmanaged memory. I cant think of a single...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.