99偷拍视频精品区一区二,口述久久久久久久久久久久,国产精品夫妇激情啪发布,成人永久免费网站在线观看,国产精品高清免费在线,青青草在线观看视频观看,久久久久久国产一区,天天婷婷久久18禁,日韩动漫av在线播放直播

C#指針的實(shí)現(xiàn)方法

本篇內(nèi)容介紹了“C#指針的實(shí)現(xiàn)方法”的有關(guān)知識,在實(shí)際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

成都創(chuàng)新互聯(lián)是專業(yè)的麒麟網(wǎng)站建設(shè)公司,麒麟接單;提供做網(wǎng)站、網(wǎng)站建設(shè),網(wǎng)頁設(shè)計,網(wǎng)站設(shè)計,建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行麒麟網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來合作!

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

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

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

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

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

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

附C#指針的實(shí)現(xiàn):

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#指針的實(shí)現(xiàn)方法”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

標(biāo)題名稱:C#指針的實(shí)現(xiàn)方法
分享地址:http://www.yijiale78.com/article24/pcssje.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計公司、網(wǎng)站維護(hù)App開發(fā)虛擬主機(jī)、微信公眾號、網(wǎng)頁設(shè)計公司

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

搜索引擎優(yōu)化