日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
C#指針使用簡析

C#指針的存在狀況簡析:指針在C\C++里面可是一個好東西,但是到java,.net的時代指針已經(jīng)被封裝起來,對用戶不可見,這點java做的非常的徹底。.net可能因為還存在一個托管C++,因此指針并沒有完全廢除,C#還是保留了指針的操作。

要使用指針首先要對使用指針的代碼用unsafe進行進行聲明,聲明和public聲明一樣,可以對整個類進行聲明,也可以是類里面某個方法或者屬性。在代碼里什么后,還需要修改工程項目的Build屬性,讓編譯器支持指針的操作。

做好事前的工作就可以使用指針了。指針的使用方法和C++下使用沒有太多差別。只要編譯器不報錯就沒有太大問題。

下面就C#指針來看其他指針的一些使用上的理解:

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

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

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

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

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

 
 
 
  1. byte[] buffer = new byte[100];  
  2. fixed (byte* p = buffer)  
  3. {  
  4.     P[0] = 123;  
  5.     ……  

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

 
 
 
  1. public unsafe class Memory  
  2.     {  
  3. // Handle for the process heap.  
  4. // This handle is used in all calls to the  
  5. // HeapXXX APIs in the methods below.  
  6. static int ph = GetProcessHeap();  
  7. // Private instance constructor to prevent instantiation.  
  8. private Memory() { }  
  9. // Allocates a memory block of the given size.   
  10. //The allocated memory is  
  11. // automatically initialized to zero.  
  12. public static void* Alloc(int size)  
  13. {  
  14.     void* result = HeapAlloc(ph, HEAP_ZERO_MEMORY, size);  
  15.     if (result == null) throw new OutOfMemoryException();  
  16.     return result;  
  17. }  
  18. // Copies count bytes from src to dst.   
  19. //The source and destination  
  20. // blocks are permitted to overlap.  
  21. public static void Copy(void* src, void* dst, int count)  
  22. {  
  23.     byte* ps = (byte*)src;  
  24.     byte* pd = (byte*)dst;  
  25.     if (ps > pd)  
  26.     {  
  27. for (; count != 0; count--) *pd++ = *ps++;  
  28.     }  
  29.     else if (ps < pd)  
  30.     {  
  31. for (ps += count, pd += count;   
  32. count != 0; count--) *--pd = *--ps;  
  33.     }  
  34. }  
  35. // Frees a memory block.  
  36. public static void Free(void* block)  
  37. {  
  38.     if (!HeapFree(ph, 0, block))   
  39. throw new InvalidOperationException();  
  40. }  
  41. // Re-allocates a memory block.   
  42. //If the reallocation request is for a  
  43. // larger size, the additional region of memory is automatically  
  44. // initialized to zero.  
  45. public static void* ReAlloc(void* block, int size)  
  46. {  
  47.     void* result = HeapReAlloc(ph, HEAP_ZERO_MEMORY, block, size);  
  48.     if (result == null) throw new OutOfMemoryException();  
  49.     return result;  
  50. }  
  51. // Returns the size of a memory block.  
  52. public static int SizeOf(void* block)  
  53. {  
  54.     int result = HeapSize(ph, 0, block);  
  55.     if (result == -1) throw new InvalidOperationException();  
  56.     return result;  
  57. }  
  58. // Heap API flags  
  59. const int HEAP_ZERO_MEMORY = 0x00000008;  
  60. // Heap API functions  
  61. [DllImport("kernel32")]  
  62. static extern int GetProcessHeap();  
  63. [DllImport("kernel32")]  
  64. static extern void* HeapAlloc(int hHeap, int flags, int size);  
  65. [DllImport("kernel32")]  
  66. static extern bool HeapFree(int hHeap, int flags, void* block);  
  67. [DllImport("kernel32")]  
  68. static extern void* HeapReAlloc(int hHeap, int flags,  
  69.    void* block, int size);  
  70. [DllImport("kernel32")]  
  71. static extern int HeapSize(int hHeap, int flags, void* block);  
  72.     } 

C#指針方面的內(nèi)容就向你介紹到這里,希望對你了解學習C#指針有所幫助。


網(wǎng)頁標題:C#指針使用簡析
本文鏈接:http://www.dlmjj.cn/article/dphiohi.html