知识大全 详细解说C#里使用指针

Posted 指针

篇首语:逆水行舟用力撑,一篙松劲退千寻。本文由小常识网(cha138.com)小编为大家整理,主要介绍了知识大全 详细解说C#里使用指针相关的知识,希望对你有一定的参考价值。

详细解说C#里使用指针  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!

      指针在C\\C++里面可是一个好东西 但是到的时代指针已经被封装起来 对用户不可见 这点java做的非常的彻底可能因为还存在一个托管C++ 因此指针并没有完全废除 C#还是保留了指针的操作        要使用指针首先要对使用指针的代码用unsafe进行进行声明 声明和public声明一样 可以对整个类进行声明 也可以是类里面某个方法或者属性 在代码里什么后 还需要修改工程项目的Build属性 让编译器支持指针的操作        做好事前的工作就可以使用指针了 指针的使用方法和C++下使用没有太多差别 只要编译器不报错就没有太大问题        下面是对指针的一些使用上的理解 .  指针类型可以是实体变量(int double)也可以是enum 同时也支持结构体变量struct 但不能是类 不过空指针可以指向类 只不过空指针不能进行任何操作 也只能把空指针作为传递对象来使用 . C#提供一个的关键字stackalloc用于申请堆栈内存 注意 这个申请内存分配的是栈内存 当函数执行完毕后 内存会被自动回收 不过我想用这个栈内存基本可以解决 %的问题 而且使用的时候不必担心内存泄漏问题 .& 好像不直接支持堆内存的申请(这个来说很危险) 不过我们可以通过调用win api 的方法进行申请 这样就可以解决剩下 %的问题 堆内存申请的方法在MSDN里面有相关的文档 具体实现代码见附 .  结构体是一个特殊的对象 他与类的定义就差一个关键字 使用方法也和类一样 可以定义属性 可以定义方法 但是在进行指针操作的时候双方就有很大的差别了 结构体可以通过sizeof()取得大小 大小与结构体里有多少实体变量有关 但是如果struck里定义了类的对象 或者指针 sizeof可能会编译不过(void* 的空指针例外 不过需要在结构体声明处加上unsafe) . fixed关键字 目前了解的不多 不过有一个很实用的例子可以让指针能够里的数组进行交互操作                   byte[] buffer = new byte[ ];                 fixed (byte* p = buffer)                                     P[ ] = ;                     ……                   .  其它 .           附     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 != ; count ) *pd++ = *ps++;                          else if (ps < pd)                             for (ps += count pd += count; count != ; count ) * pd = * ps;                             // Frees a memory block         public static void Free(void* block)                     if (!HeapFree(ph 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 block);             if (result == ) throw new InvalidOperationException();             return result;                 // Heap API flags         const int HEAP_ZERO_MEMORY = x ;         // Heap API functions         [DllImport( kernel )]         static extern int GetProcessHeap();         [DllImport( kernel )]         static extern void* HeapAlloc(int hHeap int flags int size);         [DllImport( kernel )]         static extern bool HeapFree(int hHeap int flags void* block);         [DllImport( kernel )]         static extern void* HeapReAlloc(int hHeap int flags            void* block int size);         [DllImport( kernel )]         static extern int HeapSize(int hHeap int flags void* block);     cha138/Article/program/net/201311/13152

相关参考