Csharp/CSharp Tutorial/unsafe/IntPtr — различия между версиями

Материал из .Net Framework эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Текущая версия на 15:20, 26 мая 2010

Fixing Managed Data in Memory

<source lang="csharp">using System;

public class MyClass {

   public unsafe static void Main()
   {
       int ArrayIndex;
       int [] IntegerArray;
  
       IntegerArray = new int [5];
       fixed(int * IntegerPointer = IntegerArray)
       {
           for(ArrayIndex = 0; ArrayIndex < 5; ArrayIndex++)
               IntegerPointer[ArrayIndex] = ArrayIndex;
       }
       for(ArrayIndex = 0; ArrayIndex < 5; ArrayIndex++)
           Console.WriteLine(IntegerArray[ArrayIndex]);
   }

}</source>

IntPtr.Zero

<source lang="csharp">using System; using System.Runtime.InteropServices; public class MainClass {

 [STAThread]
 static void Main(string[] args)
 {
   IntPtr ptr = IntPtr.Zero;
   ptr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(int)));
   Marshal.FreeHGlobal(ptr);
 }

}</source>