<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="ru">
		<id>http://nfex.ru/index.php?action=history&amp;feed=atom&amp;title=Csharp%2FCSharp_Tutorial%2Funsafe%2FPointer</id>
		<title>Csharp/CSharp Tutorial/unsafe/Pointer - История изменений</title>
		<link rel="self" type="application/atom+xml" href="http://nfex.ru/index.php?action=history&amp;feed=atom&amp;title=Csharp%2FCSharp_Tutorial%2Funsafe%2FPointer"/>
		<link rel="alternate" type="text/html" href="http://nfex.ru/index.php?title=Csharp/CSharp_Tutorial/unsafe/Pointer&amp;action=history"/>
		<updated>2026-04-29T18:59:57Z</updated>
		<subtitle>История изменений этой страницы в вики</subtitle>
		<generator>MediaWiki 1.30.0</generator>

	<entry>
		<id>http://nfex.ru/index.php?title=Csharp/CSharp_Tutorial/unsafe/Pointer&amp;diff=6746&amp;oldid=prev</id>
		<title> в 15:31, 26 мая 2010</title>
		<link rel="alternate" type="text/html" href="http://nfex.ru/index.php?title=Csharp/CSharp_Tutorial/unsafe/Pointer&amp;diff=6746&amp;oldid=prev"/>
				<updated>2010-05-26T15:31:53Z</updated>
		
		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left&quot; data-mw=&quot;interface&quot;&gt;
				&lt;tr style=&quot;vertical-align: top;&quot; lang=&quot;ru&quot;&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;← Предыдущая&lt;/td&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;Версия 15:31, 26 мая 2010&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; style=&quot;text-align: center;&quot; lang=&quot;ru&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;(нет различий)&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</summary>
			</entry>

	<entry>
		<id>http://nfex.ru/index.php?title=Csharp/CSharp_Tutorial/unsafe/Pointer&amp;diff=6747&amp;oldid=prev</id>
		<title>Admin: 1 версия</title>
		<link rel="alternate" type="text/html" href="http://nfex.ru/index.php?title=Csharp/CSharp_Tutorial/unsafe/Pointer&amp;diff=6747&amp;oldid=prev"/>
				<updated>2010-05-26T12:20:21Z</updated>
		
		<summary type="html">&lt;p&gt;1 версия&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Новая страница&lt;/b&gt;&lt;/p&gt;&lt;div&gt;==An array name with an index yields a pointer to the start of the array==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;using System; &lt;br /&gt;
 &lt;br /&gt;
class MainClass{ &lt;br /&gt;
  unsafe public static void Main() { &lt;br /&gt;
    int[] nums = new int[10]; &lt;br /&gt;
 &lt;br /&gt;
    fixed(int* p = &amp;amp;nums[0], p2 = nums) { &lt;br /&gt;
      if(p == p2) &lt;br /&gt;
        Console.WriteLine(&amp;quot;p and p2 point to same address.&amp;quot;); &lt;br /&gt;
    } &lt;br /&gt;
  } &lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;p and p2 point to same address.&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Assign value of myInt using pointer==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;using System;&lt;br /&gt;
using System.Globalization;&lt;br /&gt;
&lt;br /&gt;
public class MainClass{&lt;br /&gt;
  public static void SomeUnsafeCode()&lt;br /&gt;
  {&lt;br /&gt;
    unsafe&lt;br /&gt;
    {&lt;br /&gt;
      int myInt;&lt;br /&gt;
      int* ptrToMyInt = &amp;amp;myInt;&lt;br /&gt;
      &lt;br /&gt;
      // Assign value of myInt using pointer.&lt;br /&gt;
      *ptrToMyInt = 123;  &lt;br /&gt;
      // print out address.&lt;br /&gt;
      Console.WriteLine(&amp;quot;Value of myInt {0}&amp;quot;, myInt);&lt;br /&gt;
      Console.WriteLine(&amp;quot;Address of myInt {0:X}&amp;quot;, (int)ptrToMyInt);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  static void Main(string[] args)&lt;br /&gt;
  {&lt;br /&gt;
        SomeUnsafeCode();&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;Value of myInt 123&lt;br /&gt;
Address of myInt 12F474&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==C# Pointer Operators==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;Operator      Description&lt;br /&gt;
&amp;amp;             returns a pointer that represents the memory address of the variable.&lt;br /&gt;
*             to declare a variable to be a pointer of some type, &lt;br /&gt;
              and to dereference the pointer value to get to the value of the variable pointed to by the pointer.&lt;br /&gt;
-&amp;gt;            dereferencing and member access operator.&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Declaring a Pointer==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;The general form of a pointer variable declaration is&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;type* var-name; &lt;br /&gt;
int* ip;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Demonstrate the effects of pointer arithmethic==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;using System; &lt;br /&gt;
 &lt;br /&gt;
class MainClass { &lt;br /&gt;
  unsafe public static void Main() { &lt;br /&gt;
    int i = 10;  &lt;br /&gt;
    double d = 10.0; &lt;br /&gt;
 &lt;br /&gt;
    int* ip = &amp;amp;i; &lt;br /&gt;
    double* fp = &amp;amp;d; &lt;br /&gt;
 &lt;br /&gt;
    for(int x=0; x &amp;lt; 10; x++) { &lt;br /&gt;
       Console.WriteLine((uint) (ip) + &amp;quot; &amp;quot; + &lt;br /&gt;
                         (uint) (fp)); &lt;br /&gt;
       ip++; &lt;br /&gt;
       fp++; &lt;br /&gt;
    } &lt;br /&gt;
  } &lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;1242208 1242212&lt;br /&gt;
1242212 1242220&lt;br /&gt;
1242216 1242228&lt;br /&gt;
1242220 1242236&lt;br /&gt;
1242224 1242244&lt;br /&gt;
1242228 1242252&lt;br /&gt;
1242232 1242260&lt;br /&gt;
1242236 1242268&lt;br /&gt;
1242240 1242276&lt;br /&gt;
1242244 1242284&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Get a pointer to the start of a string and display the contents of string via pointer==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;using System; &lt;br /&gt;
 &lt;br /&gt;
class MainClass { &lt;br /&gt;
  unsafe public static void Main() { &lt;br /&gt;
    string str = &amp;quot;this is a test&amp;quot;; &lt;br /&gt;
 &lt;br /&gt;
    fixed(char* p = str) { &lt;br /&gt;
      for(int i=0; p[i] != 0; i++) &lt;br /&gt;
        Console.Write(p[i]); &lt;br /&gt;
    } &lt;br /&gt;
    Console.WriteLine(); &lt;br /&gt;
  } &lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;this is a test&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Index pointer==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;using System; &lt;br /&gt;
 &lt;br /&gt;
class MainClass { &lt;br /&gt;
  unsafe public static void Main() { &lt;br /&gt;
    int[] nums = new int[10]; &lt;br /&gt;
 &lt;br /&gt;
    &lt;br /&gt;
    Console.WriteLine(&amp;quot;Index pointer like array.&amp;quot;); &lt;br /&gt;
    fixed (int* p = nums) { &lt;br /&gt;
      for(int i=0; i &amp;lt; 10; i++)  &lt;br /&gt;
        p[i] = i; // index pointer like array &lt;br /&gt;
 &lt;br /&gt;
      for(int i=0; i &amp;lt; 10; i++)  &lt;br /&gt;
        Console.WriteLine(&amp;quot;p[{0}]: {1} &amp;quot;, i, p[i]); &lt;br /&gt;
    } &lt;br /&gt;
 &lt;br /&gt;
  } &lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;Index pointer like array.&lt;br /&gt;
p[0]: 0&lt;br /&gt;
p[1]: 1&lt;br /&gt;
p[2]: 2&lt;br /&gt;
p[3]: 3&lt;br /&gt;
p[4]: 4&lt;br /&gt;
p[5]: 5&lt;br /&gt;
p[6]: 6&lt;br /&gt;
p[7]: 7&lt;br /&gt;
p[8]: 8&lt;br /&gt;
p[9]: 9&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==IntPtrs and unsafe bit-twiddling==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Pointers are variables that hold the addresses of other variables.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;using System;&lt;br /&gt;
using System.IO;&lt;br /&gt;
using System.Reflection;&lt;br /&gt;
using System.Runtime.ConstrainedExecution;&lt;br /&gt;
using System.Runtime.InteropServices;&lt;br /&gt;
using Microsoft.Win32.SafeHandles;&lt;br /&gt;
public class MainClass&lt;br /&gt;
{&lt;br /&gt;
    public unsafe static void Main()&lt;br /&gt;
    {&lt;br /&gt;
        int a = 10;&lt;br /&gt;
        Console.WriteLine(&amp;quot;a is {0} ({0:X})&amp;quot;, a);&lt;br /&gt;
        IntPtr ip = new IntPtr(&amp;amp;a);&lt;br /&gt;
        byte* pTarget = (byte*)ip.ToPointer() + 1;&lt;br /&gt;
        *pTarget = 2;&lt;br /&gt;
        Console.WriteLine(&amp;quot;a is {0} ({0:X})&amp;quot;, a);&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;a is 10 (A)&lt;br /&gt;
a is 522 (20A)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Modifying an Immutable String with Pointer==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;class MainClass&lt;br /&gt;
{&lt;br /&gt;
  static void Main()&lt;br /&gt;
  {&lt;br /&gt;
    string text = &amp;quot;S5280ft&amp;quot;;&lt;br /&gt;
    System.Console.Write(&amp;quot;{0} = &amp;quot;, text);&lt;br /&gt;
    unsafe{&lt;br /&gt;
    fixed (char* pText = text)&lt;br /&gt;
      {&lt;br /&gt;
          char* p = pText;&lt;br /&gt;
          *++p = &amp;quot;m&amp;quot;;&lt;br /&gt;
          *++p = &amp;quot;i&amp;quot;;&lt;br /&gt;
          *++p = &amp;quot;l&amp;quot;;&lt;br /&gt;
          *++p = &amp;quot;e&amp;quot;;&lt;br /&gt;
          *++p = &amp;quot;A&amp;quot;;&lt;br /&gt;
          *++p = &amp;quot;B&amp;quot;;&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
    System.Console.WriteLine(text);&lt;br /&gt;
    &lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Pointer arithmetic==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;using System; &lt;br /&gt;
 &lt;br /&gt;
class MainClass { &lt;br /&gt;
  unsafe public static void Main() { &lt;br /&gt;
    int[] nums = new int[10]; &lt;br /&gt;
 &lt;br /&gt;
    Console.WriteLine(&amp;quot;\nUse pointer arithmetic.&amp;quot;); &lt;br /&gt;
    fixed (int* p = nums) { &lt;br /&gt;
      for(int i=0; i &amp;lt; 10; i++)  &lt;br /&gt;
        *(p+i) = i;&lt;br /&gt;
 &lt;br /&gt;
      for(int i=0; i &amp;lt; 10; i++)  &lt;br /&gt;
        Console.WriteLine(&amp;quot;*(p+{0}): {1} &amp;quot;, i, *(p+i)); &lt;br /&gt;
    } &lt;br /&gt;
  } &lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;Use pointer arithmetic.&lt;br /&gt;
*(p+0): 0&lt;br /&gt;
*(p+1): 1&lt;br /&gt;
*(p+2): 2&lt;br /&gt;
*(p+3): 3&lt;br /&gt;
*(p+4): 4&lt;br /&gt;
*(p+5): 5&lt;br /&gt;
*(p+6): 6&lt;br /&gt;
*(p+7): 7&lt;br /&gt;
*(p+8): 8&lt;br /&gt;
*(p+9): 9&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Pointer of a pointer==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;using System; &lt;br /&gt;
 &lt;br /&gt;
class MainClass { &lt;br /&gt;
  unsafe public static void Main() { &lt;br /&gt;
    int x;    // an int value  &lt;br /&gt;
    int* p;   // an int pointer &lt;br /&gt;
    int** q;  // an pointer to an int pointer &lt;br /&gt;
 &lt;br /&gt;
    x = 10; &lt;br /&gt;
    p = &amp;amp;x; &lt;br /&gt;
    q = &amp;amp;p; &lt;br /&gt;
 &lt;br /&gt;
    Console.WriteLine(**q); &lt;br /&gt;
  } &lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;10&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Safe Swap==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;using System;&lt;br /&gt;
using System.Globalization;&lt;br /&gt;
public class MainClass{&lt;br /&gt;
  public static void SafeSwap(ref int i, ref int j)&lt;br /&gt;
  {&lt;br /&gt;
    int temp = i;&lt;br /&gt;
    i = j;&lt;br /&gt;
    j = temp;&lt;br /&gt;
  }&lt;br /&gt;
  static void Main(string[] args)&lt;br /&gt;
  {&lt;br /&gt;
    int i = 10, j = 20;&lt;br /&gt;
    &lt;br /&gt;
    Console.WriteLine(&amp;quot;Values before safe swap: i = {0}, j = {1}&amp;quot;, i, j);&lt;br /&gt;
    SafeSwap(ref i, ref j);&lt;br /&gt;
    Console.WriteLine(&amp;quot;Values after safe swap: i = {0}, j = {1}&amp;quot;, i, j);&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;Values before safe swap: i = 10, j = 20&lt;br /&gt;
Values after safe swap: i = 20, j = 10&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==The &amp;amp; is a unary operator that returns the memory address of its operand==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;using System;&lt;br /&gt;
class MainClass&lt;br /&gt;
{&lt;br /&gt;
  static unsafe void print( char* p, int len )&lt;br /&gt;
  {&lt;br /&gt;
    for ( int i = 0; i &amp;lt; len; i++, p++ )&lt;br /&gt;
    {&lt;br /&gt;
      Console.WriteLine( *p );&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  [STAThread]&lt;br /&gt;
  static unsafe void Main(string[] args)&lt;br /&gt;
  {&lt;br /&gt;
    string s = &amp;quot;unsafe code&amp;quot;;&lt;br /&gt;
    fixed ( char* p = &amp;amp;( s.ToCharArray()[ 0 ] ) )&lt;br /&gt;
    {&lt;br /&gt;
      print ( p, s.Length );&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;u&lt;br /&gt;
n&lt;br /&gt;
s&lt;br /&gt;
a&lt;br /&gt;
f&lt;br /&gt;
e&lt;br /&gt;
c&lt;br /&gt;
o&lt;br /&gt;
d&lt;br /&gt;
e&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Unsafe swap==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;using System;&lt;br /&gt;
using System.Globalization;&lt;br /&gt;
public class MainClass{&lt;br /&gt;
  unsafe public static void UnsafeSwap(int* i, int* j)&lt;br /&gt;
  {&lt;br /&gt;
    int temp = *i;&lt;br /&gt;
    *i = *j;&lt;br /&gt;
    *j = temp;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  static void Main(string[] args)&lt;br /&gt;
  {&lt;br /&gt;
    int i = 10, j = 20;&lt;br /&gt;
    &lt;br /&gt;
    Console.WriteLine(&amp;quot;Unsafe swap&amp;quot;);&lt;br /&gt;
    Console.WriteLine(&amp;quot;Values before unsafe swap: i = {0}, j = {1}&amp;quot;, i, j);&lt;br /&gt;
    unsafe{ UnsafeSwap(&amp;amp;i, &amp;amp;j);} &lt;br /&gt;
    Console.WriteLine(&amp;quot;Values after unsafe swap: i = {0}, j = {1}&amp;quot;, i, j);&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;Unsafe swap&lt;br /&gt;
Values before unsafe swap: i = 10, j = 20&lt;br /&gt;
Values after unsafe swap: i = 20, j = 10&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Use fixed to put address of value into a pointer==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;using System; &lt;br /&gt;
 &lt;br /&gt;
class MyClass { &lt;br /&gt;
  public int num; &lt;br /&gt;
  public MyClass(int i) { num = i; } &lt;br /&gt;
} &lt;br /&gt;
 &lt;br /&gt;
class FixedCode { &lt;br /&gt;
  unsafe public static void Main() { &lt;br /&gt;
    MyClass o = new MyClass(19); &lt;br /&gt;
 &lt;br /&gt;
    fixed (int* p = &amp;amp;o.num) {  &lt;br /&gt;
 &lt;br /&gt;
      Console.WriteLine(&amp;quot;Initial value of o.num is &amp;quot; + *p); &lt;br /&gt;
   &lt;br /&gt;
      *p = 10; // assign the to count via p &lt;br /&gt;
     &lt;br /&gt;
      Console.WriteLine(&amp;quot;New value of o.num is &amp;quot; + *p); &lt;br /&gt;
    } &lt;br /&gt;
  } &lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;Initial value of o.num is 19&lt;br /&gt;
New value of o.num is 10&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Admin</name></author>	</entry>

	</feed>