日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

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

新聞中心

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

C#指針的存在狀況簡(jiǎn)析:指針在C\C++里面可是一個(gè)好東西,但是到j(luò)ava,.net的時(shí)代指針已經(jīng)被封裝起來(lái),對(duì)用戶不可見(jiàn),這點(diǎn)java做的非常的徹底。.net可能因?yàn)檫€存在一個(gè)托管C++,因此指針并沒(méi)有完全廢除,C#還是保留了指針的操作。

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

做好事前的工作就可以使用指針了。指針的使用方法和C++下使用沒(méi)有太多差別。只要編譯器不報(bào)錯(cuò)就沒(méi)有太大問(wèn)題。

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

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

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

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

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

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

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

附C#指針的實(shí)現(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#指針?lè)矫娴膬?nèi)容就向你介紹到這里,希望對(duì)你了解學(xué)習(xí)C#指針有所幫助。


本文名稱:C#指針使用簡(jiǎn)析
網(wǎng)頁(yè)網(wǎng)址:http://www.5511xx.com/article/dphiohi.html