C#指针的实现方法

发布时间:2021-08-25 17:25:13 作者:chen
来源:亿速云 阅读:136

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

推荐阅读:
  1. c#中一级指针,二级指针,指针数组,数组和指针的示例分析
  2. C# 隐藏、显示鼠标指针

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

上一篇:c#中的string是不能修改的吗

下一篇:C#实现文件上传功能代码分享

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》