C#学习日记 指针

在C#里使用指针要把使用到指针的代码放入unsafe块中。垃圾回收器是处理不了指针的,所以指针不能指向类的实例,但是可以使用fixed关键词,告诉垃圾回收器有指针指向这个对象,在作用域范围内,该对象不能被移动。结构是值类型的,所以可以使用指针。编译语句csc /unsafe XXX.cs,使用指针要小心,编译器不会检查溢出checked关键词无效。指针存在栈上,所以很高效。

using System;

namespace Sample
{
	unsafe class MainEntryPoint
	{
		static int Main(string[] args)
		{
			/*int x = 10;
			int* pX = &x;
			Console.WriteLine("Value X:{0}", x);
			Console.WriteLine("Address X:{0}", (uint)&x);
			Console.WriteLine("Value pX:{0}", (uint)pX);
			Console.WriteLine("Address pX:{0}", (uint)&pX);
			Console.WriteLine("Value Address = pX :{0}", *pX);*/

			/*int x = 10;
			double y = 20;
			int* pX = &x;
			double* pY = &y;
			Console.WriteLine("Adress X:{0:X}", (uint)&x);
			Console.WriteLine("Adress Y:{0:X}", (uint)&y);
			Console.WriteLine("Adress pX:{0:X}", (uint)pX);
			Console.WriteLine("Adress pY:{0:X}", (uint)pY);

			Console.WriteLine("After:");
			pX ++;
			pY ++;
			pY += 3;
			Console.WriteLine("Adress X:{0:X}", (uint)&x);
			Console.WriteLine("Adress Y:{0:X}", (uint)&y);
			Console.WriteLine("Adress pX:{0:X}", (uint)pX);
			Console.WriteLine("Adress pY:{0:X}", (uint)pY);*/

			MyClass myClass = new MyClass();
			//指针禁止指向类实例
			//因为垃圾回收器不能处理指针 如果引用的对象被移动 指针会出现错误
			//使用fixed关键词 在作用域内告诉垃圾回收器 该对象不能移动
			//指针就可以指向对象的成员
			fixed(long* pObject = &(myClass.X))
			{
			}
			Console.WriteLine("CurrencyStruct size is {0}", sizeof(CurrencyStruct));
			CurrencyStruct amount1, amount2;
			CurrencyStruct* pAmount1 = &amount1;
			long* pDollars = &(pAmount1 -> Dollars);
			byte* pCents = &(pAmount1 -> Cents);
			Console.WriteLine("Adress of amount1 is 0x{0:X}", (uint)&amount1);
			Console.WriteLine("Adress of pAmount1 is 0x{0:X}", (uint)&pAmount1);
			Console.WriteLine("Adress of pDollars is 0x{0:X}", (uint)&pDollars);
			Console.WriteLine("Adress of pCents is 0x{0:X}", (uint)&pCents);

			*pDollars = 20;
			*pCents = 50;
			Console.WriteLine("amount1 contains:{0}", amount1);
			return 0;
		}
	}
	public class MyClass
	{
		public long X;
		public double F;
	}
	internal struct CurrencyStruct
	{
		public long Dollars;
		public byte Cents;
		public override string ToString()
		{
			return "$"+Dollars+"."+Cents;
		}
	}
	internal class CurrencyClass
	{
		public long Dollars;
		public byte Cents;
		public override string ToString()
		{
			return "$"+Dollars+"."+Cents;
		}
	}
}

可以用指针创建数组,也可以使用索引,但是越界不会抛异常。

using System;

namespace Sample
{
	internal class MainEntryPoint
	{
		private static unsafe void Main()
		{
			Console.Write("How big an array do you want?\n>");
			string input = Console.ReadLine();
			uint size = uint.Parse(input);

			long* pArray = stackalloc long[(int) size];
			for(int i = 0; i < size; i++)
			{
				pArray[i] = i;
			}
			for(int i = 0; i < size; i++)
			{
				Console.WriteLine(pArray[i]);
			}
			Console.ReadLine();
		}
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值