溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

C#指針的實現方法

發布時間:2021-08-25 17:25:13 來源:億速云 閱讀:194 作者:chen 欄目:編程語言

本篇內容介紹了“C#指針的實現方法”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!

1. 指針類型可以是實體變量(int,double)也可以是enum,同時也支持結構體變量struct。但不能是類。不過空指針可以指向類,只不過空指針不能進行任何操作,也只能把空指針作為傳遞對象來使用。

2. C#提供一個的關鍵字stackalloc用于申請堆棧內存。注意,這個申請內存分配的是棧內存,當函數執行完畢后,內存會被自動回收。不過我想用這個棧內存基本可以解決40%的問題,而且使用的時候不必擔心內存泄漏問題。

3. .net 好像不直接支持堆內存的申請(這個對.net來說很危險),不過我們可以通過調用win32 api 的方法進行申請。這樣就可以解決剩下40%的問題。堆內存申請的方法在MSDN里面有相關的文檔,具體實現代碼見附。

4.  結構體是一個特殊的對象。他與類的定義就差一個關鍵字,使用方法也和類一樣,可以定義屬性,可以定義方法。但是在進行指針操作的時候雙方就有很大的差別了。結構體可以通過sizeof()取得大小,大小與結構體里有多少實體變量有關,但是如果struck里定義了類的對象,或者指針,sizeof可能會編譯不過(void* 的空指針例外,不過需要在結構體聲明處加上unsafe)。

5. fixed關鍵字:目前了解的不多,不過有一個很實用的例子可以讓指針能夠和.net里的數組進行交互操作:

byte[] buffer = new byte[100];  fixed (byte* p = buffer)  {      P[0] = 123;      ……  }

附C#指針的實現:

public unsafe class Memory      {  // Handle for the process heap.  // This handle is used in all calls to the  // HeapXXX APIs in the methods below.  static int ph = GetProcessHeap();  // Private instance constructor to prevent instantiation.  private Memory() { }  // Allocates a memory block of the given size.   //The allocated memory is  // automatically initialized to zero.  public static void* Alloc(int size)  {      void* result = HeapAlloc(ph, HEAP_ZERO_MEMORY, size);      if (result == null) throw new OutOfMemoryException();      return result;  }  // Copies count bytes from src to dst.   //The source and destination  // blocks are permitted to overlap.  public static void Copy(void* src, void* dst, int count)  {      byte* ps = (byte*)src;      byte* pd = (byte*)dst;      if (ps > pd)      {  for (; count != 0; count--) *pd++ = *ps++;      }      else if (ps < pd)      {  for (ps += count, pd += count;   count != 0; count--) *--pd = *--ps;      }  }  // Frees a memory block.  public static void Free(void* block)  {      if (!HeapFree(ph, 0, block))   throw new InvalidOperationException();  }  // Re-allocates a memory block.   //If the reallocation request is for a  // larger size, the additional region of memory is automatically  // initialized to zero.  public static void* ReAlloc(void* block, int size)  {      void* result = HeapReAlloc(ph, HEAP_ZERO_MEMORY, block, size);      if (result == null) throw new OutOfMemoryException();      return result;  }  // Returns the size of a memory block.  public static int SizeOf(void* block)  {      int result = HeapSize(ph, 0, block);      if (result == -1) throw new InvalidOperationException();      return result;  }  // Heap API flags  const int HEAP_ZERO_MEMORY = 0x00000008;  // Heap API functions  [DllImport("kernel32")]  static extern int GetProcessHeap();  [DllImport("kernel32")]  static extern void* HeapAlloc(int hHeap, int flags, int size);  [DllImport("kernel32")]  static extern bool HeapFree(int hHeap, int flags, void* block);  [DllImport("kernel32")]  static extern void* HeapReAlloc(int hHeap, int flags,     void* block, int size);  [DllImport("kernel32")]  static extern int HeapSize(int hHeap, int flags, void* block);      }

“C#指針的實現方法”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女