How to: Declare Pinning Pointers and Value Types

 

The new home for Visual Studio documentation is Visual Studio 2017 Documentation on docs.microsoft.com.

A value type can be implicitly boxed. You can then declare a pinning pointer to the value type object itself and use a pin_ptr to the boxed value type.

Code

// pin_ptr_value.cpp  
// compile with: /clr  
value struct V {  
   int i;  
};  
  
int main() {  
   V ^ v = gcnew V;   // imnplicit boxing  
   v->i=8;  
   System::Console::WriteLine(v->i);  
   pin_ptr<V> mv = &*v;  
   mv->i = 7;  
   System::Console::WriteLine(v->i);  
   System::Console::WriteLine(mv->i);  
}  

Output

8  
7  
7  

pin_ptr (C++/CLI)

Show: