473,725 Members | 2,032 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

1 New Member
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 2492
IanWright
179 New Member
Honestly I have no idea. I would recommend you try some Mono forums
Jun 25 '09 #2
r035198x
13,262 MVP
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
14936
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 code I get the error "You canceled the previous operation" err msg. When clicking on Help I get a white screen. I haven't got the slightest idea what it means .... I pinned down that this error comes from the Dlookup function, because if I put...
2
1433
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
14502
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 program (UNIX) as an array of "struct point" where struct point //C structure { int Time; //32 bits
12
8125
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 raw MMX and SSE power, the starting pointer MUST be starting at a 16 byte memory boundary. In C++ I allocate more memory than needed, and in a second phase I search for the address that starts on a 16 byte boundary. And I then use that new...
2
11198
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 a VB .NET application which doesn't have the ability to prevent the array from being moved. Thanks in advance, coz public class Wrapper {
7
1318
by: Ioannis Vranos | last post by:
Consider the code: wchar_t __pin *p= &(someCommand->ToCharArray()); _wsystem(p); p=0;
17
2409
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, if I replace '*' with '^' what do I replace '&' with to generate a 'pointer' (is 'x^' called a 'reference' per chance?) to the instance? That is:
25
3018
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 MyClass:IDisposable
16
3131
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 reason, why the GC should not allow this. It would be also great, if the memory could already be allocated as pinned:
0
8888
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8752
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9401
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9111
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4517
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4782
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3221
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2634
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2157
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.