在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { struct XYZ { public int a; public int b; public int c; bool b1; }; class Program { //静态变量存储在堆上,查看指针时需用fixed固定 static int m_sZ = 100; //普通数据成员,也是放在堆上了,查看指针时需用fixed固定 int m_nData = 100; //等价于C/C++的 #define 语句,不分配内存 const int PI = 31415; //unsafe可以放在Main函数声明中!! static unsafe void Main(string[] args) { //简单的结构变量放在栈上,无需fixed XYZ stData = new XYZ(); stData.a = 100; Console.WriteLine("结构变量= 0x{0:x}", (int)&stData); //数组变量的声明放在了栈上,数据放在了堆上,需用fixed固定 int[] arry = null; arry = new int[10]; fixed(int *p = arry) { Console.WriteLine("array = 0x{0:x}", (int)p); } //这些放在栈上的变量,可以直接使用指针指向 //从打印的指针的数据看,int是4字节的,double是8字节的 int y = 10; int z = 100; double f = 0.90; Console.WriteLine("本地变量y = 0x{0:X}, z = 0x{1:X}", (int)&y, (int)&z); Console.WriteLine("本地变量f = 0x{0:X}", (int)&f); //下面失败 //fixed (int* p = &P.PI) //{ //} //放在堆里面的数据的地址,就必须用fixed语句! string ss = "Helo"; fixed(char *p = ss) { Console.WriteLine("字符串地址= 0x{0:x}", (int)p); } Program P = new Program(); //这个是类对象,放在堆里面 fixed(int *p = &P.m_nData) { Console.WriteLine("普通类成员变量 = 0x{0:X}", (int)p); } //静态成员变量在堆上 fixed(int *p = &m_sZ) { Console.WriteLine("静态成员变量 = 0x{0:X}", (int)p); } //下面是每种类型的占用字节个数 Console.Write("\n\n下面是每种类型的占用字节个数\n"); Console.WriteLine("sizeof(void *) = {0}", sizeof(void*)); Console.WriteLine("sizeof(int) = {0}, * = {1}", sizeof(int), sizeof(int*));//4 Console.WriteLine("sizeof(long) = {0}, * = {1}", sizeof(long), sizeof(long*));//8 Console.WriteLine("sizeof(byte) = {0}, * = {1}", sizeof(byte), sizeof(byte*));//1 Console.WriteLine("sizeof(bool) = {0}, * = {1}", sizeof(bool), sizeof(bool*));//1 Console.WriteLine("sizeof(float) = {0}, * = {1}", sizeof(float), sizeof(float*));//4 Console.WriteLine("sizeof(double) = {0}, * = {1}", sizeof(double), sizeof(double*));//8 Console.WriteLine("sizeof(decimal) = {0}, * = {1}", sizeof(decimal), sizeof(decimal*));//16 Console.WriteLine("sizeof(char) = {0}, * = {1}", sizeof(char), sizeof(char*));// Console.WriteLine("sizeof(XYZ) = {0}, * = {1}", sizeof(XYZ), sizeof(XYZ*));// //Console.WriteLine("sizeof(object) = {0}, * = {1}", sizeof(object), sizeof(object*));//16 //Console.WriteLine("sizeof(C) = {0}, * = {1}", sizeof(C), sizeof(C*));//16 } } } 输出结果: |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论