==========================================示例一
结论:
1,栈中的内存块按4的倍数进行分配(不是按照类型的大小分配的)
2,指针类型强制转换,得到的内容将无意义
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication2 { class Program { static unsafe void Main(string[] args) { //1,栈中的内存块按4的倍数进行分配(不是按照类型的大小分配的) //2,指针类型强制转换,得到的内容将无意义 int x = 10; short y = -1; byte y2 = 4; double z = 10.1; int* pX; short* pY; double* pZ; pX = &x; pY = &y; pZ = &z; //6B7E738 4 10 Console.WriteLin("address of x is {0:X},size is {1},value is {2}", (uint)&x, sizeof(int), x); //6B7E734 2 -1 Console.WriteLine("address of y is {0:X},size is {1},value is {2}", (uint)&y, sizeof(short), y); //6B7E730 1 4 Console.WriteLine("address of y2 is {0:X},size is {1},value is {2}", (uint)&y2, sizeof(byte), y2); //6B7E728 8 10.1 Console.WriteLine("address of z is {0:X},size is {1},value is {2}", (uint)&z, sizeof(double), z); //6B7E724 4 6B7E738 (取的指针的地址) Console.WriteLine("address of pX is {0:X},size is {1},value is {2:X}", (uint)&pX, sizeof(int*), (uint)pX); //6B7E720 4 6B7E734 (取的指针的地址) Console.WriteLine("address of pY is {0:X},size is {1},value is {2:X}", (uint)&pY, sizeof(short*), (uint)pY); //6B7E71C 4 6B7E728 (取的指针的地址) Console.WriteLine("address of pZ is {0:X},size is {1},value is {2:X}", (uint)&pZ, sizeof(double*), (uint)pZ); //改变了x的值 *pX = 20; //x=20,*pX=20 //指针强制装换,得到的值将无意义 pZ = (double*)pX;//*pZ的内容无意义 Console.ReadKey(); } } }
==========================================示例二
结论:
1,使用算法,改变指针
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1 { class Program { static unsafe void Main(string[] args) { CurrencyStruce amount1, amount2; CurrencyStruce* pAmount = &amount1; long* pDollars = &(pAmount->Dollars); byte* pCents = &((*pAmount).cents); //642ED1C 16 Console.WriteLine("address of amount1 is {0:X},size is {1}", (uint)&amount1, sizeof(CurrencyStruce)); //642ED0C 16 Console.WriteLine("address of amount2 is {0:X},size is {1}", (uint)&amount2, sizeof(CurrencyStruce)); //642ED08 4 642ED1C Console.WriteLine("address of pAmount is {0:X},size is {1},value is {2:X}", (uint)&pAmount, sizeof(CurrencyStruce*), (uint)pAmount); //642ED04 4 642ED1C Console.WriteLine("address of pDollars is {0:X},size is {1},value is {2:X}", (uint)&pDollars, sizeof(CurrencyStruce*), (uint)pDollars); //642ED00 4 642ED24 Console.WriteLine("address of pCents is {0:X},size is {1},value is {2:X}", (uint)&pCents, sizeof(CurrencyStruce*), (uint)pCents); *pDollars = 20; //$20.0 $0.0 Console.WriteLine("amount1 is {0},amount2 is {1}", amount1, amount2); //=================pAmount指针递减之后,其地址指向amount2(因为amount2栈中的地址在amount1的下面) pAmount--; //642ED08 4 642ED0C Console.WriteLine("address of pAmount is {0:X},size is {1},value is {2:X}", (uint)&pAmount, sizeof(CurrencyStruce*), (uint)pAmount); //=================pCents目前指向amount1.cents,使其指向amount2.cents CurrencyStruce* pTempCurrency = (CurrencyStruce*)pCents;//pTempCurrency指针指向642ED24 pCents = (byte*)(--pTempCurrency);//pTempCurrency-1(因为CurrencyStruce占用16个字节)所以结果指向642ED14 *pCents = 3; //$20.0 $0.3 Console.WriteLine("amount1 is {0},amount2 is {1}", amount1, amount2); //=================查看类成员指针 CurrentClass c = new CurrentClass(); fixed (long* pCDollars = &(c.Dollars)) fixed (byte* pCCents = &(c.cents)) { Console.WriteLine("address of c.Dollars is {0:X}",(uint)pCDollars); Console.WriteLine("address of c.cents is {0:X}", (uint)pCCents); *pCCents = 9; Console.WriteLine(c);//输出:$0.9 } Console.ReadKey(); } } //结构 struct CurrencyStruce { public long Dollars; public byte cents; public override string ToString() { return "$" + Dollars + "." + cents; } } //类 class CurrentClass { public long Dollars; public byte cents; public override string ToString() { return "$" + Dollars + "." + cents; } } }
本文出自 “程序猿的家--Hunter” 博客,请务必保留此出处http://962410314.blog.51cto.com/7563109/1567953
原文地址:http://962410314.blog.51cto.com/7563109/1567953