RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
C#指针的实现方法

本篇内容介绍了“C#指针的实现方法”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

成都创新互联是专业的麒麟网站建设公司,麒麟接单;提供做网站、网站建设,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行麒麟网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!

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#指针的实现方法”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注创新互联网站,小编将为大家输出更多高质量的实用文章!


网站名称:C#指针的实现方法
文章网址:http://sczitong.cn/article/pcssje.html