(五十五)C#编程基础复习——C#指针变量与unsafe

为了保持类型的安全性,默认情况下C#是不支持指针的,但是如果使用unsafe关键字来修饰类或类中的成员,这样的类或类中的成员就会被视为不安全代码,C#允许在不安全代码中使用指针变量。在公共语言运行时(CLR)中,不安全代码是指无法验证的代码,不安全代码不一定是危险的,只是公共语言运行时(CLR)无法验证代码的安全性。因此CLR仅会执行新程序集中包含的不安全代码。

一、指针变量

在C#中,指针同样是一个变量,但是它的值是另一个变量的内存地址,在使用指针之前我们同样需要先声明指针,声明指针的语法格式如下:

type * var_name;

下表中列举了一些定义指针的示例:

与声明变量相同,我们同样可以在一行代码中同时声明多个指针,如下所示:

int * p1,p2,p3;//定义p1、p2、p3三个整数指针

注意:指针类型不能从对象中继承,并且装箱和拆箱也不支持指针,但是不同的指针类型以及指针与整数之间可以进行转换。

示例如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _051
{
    internal class Program
    {
        static unsafe void Main(string[] args)
        {
            double f = 3.1415;
            double* p = &f;
            Console.WriteLine("数据的内容是:{0}",f);
            Console.WriteLine("数据在内存中的地址是:{0}", (int)p) ;
            Console.ReadKey();
        }
    }
}

二、使用指针检索数据的值

在C#中,我们可以使用ToString()来获取指针变量所指向的数据的值,如下所示:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _051
{
    internal class Program
    {
        static unsafe void Main(string[] args)
        {

            unsafe
            {
                int var = 12345;
                int* p = &var;
                Console.WriteLine("变量var的值为:{0}",var);
                Console.WriteLine("指针p指向的值为:{0}", p->ToString()) ;
                Console.WriteLine("指针p的值为:{0}",(int)p);
            }
            Console.ReadKey();
        }
    }
}

三、将指针作为参数传递给函数

我们可以将指针变量作为参数传递给函数,示例如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _052
{
    internal class Program
    {
        public unsafe void swap(int* p,int *q)
        {
            int temp = *p;
            *p = *q;
            *q = temp;
        }
        static unsafe void Main(string[] args)
        {
            Program p = new Program();
            int var1 = 10;
            int var2 = 20;
            int* x = &var1;
            int* y = &var2;
            Console.WriteLine("调用Swap的函数前:var1:{0},var2:{1}",var1,var2);
            p.swap(x, y);
            Console.WriteLine("调用Swap的函数后:var1:{0},var2:{1}",var1,var2);
            Console.ReadKey();
        }
    }
}

四、使用指针访问数组元素

在C#中,数组和指向该数组且与数组名称相同的指针是不同的数据类型,例如int* p和int [] p就是不同的数据类型。你可以增加指针变量p的值,因为它在内存中不是固定的,但数组地址在内存中是固定的,因此你不能增加数组p的值。如果你需要使用指针变量访问数组数据,可以像我们在C或C++中所做的那样,使用fixed关键字来固定指针。下面通过示例演示下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _053
{
    internal class Program
    {
        static unsafe void Main(string[] args)
        {
            int[] list = { 10, 100, 200 };
            fixed (int* ptr = list)
                /*显示指针中数组地址*/
                for (int i = 0; i < 3; i++)
                {
                    Console.WriteLine("list[{0}]的内存地址为:{1}", i, (int)(ptr + i));
                    Console.WriteLine("list[{0}]的值为:{1}",i,*(ptr+i));
                }
            Console.ReadKey();
        }
    }
}

五、编译不安全代码

为了编译不安全代码,在编译时必须使用unsafe命令,例如编译包含不安全代码的demo.cs程序的命令如下:

csc /unsafe demo.cs

csc -unsafe demo.cs

如果您使用的是 Visual Studio,那么您需要在项目属性中启用不安全代码,具体步骤如下:

  • 通过双击资源管理器(Solution Explorer)中的属性(properties)节点,打开项目属性(project properties);
  • 点击 Build 标签页;
  • 选择选项“Allow unsafe code”。
posted @ 2024-01-09 10:26  代号六零一  阅读(8)  评论(0编辑  收藏  举报