码迷,mamicode.com
首页 > Windows程序 > 详细

C#查看各种变量的指针地址

时间:2015-07-02 23:51:58      阅读:194      评论:0      收藏:0      [点我收藏+]

标签:

将项目的“可编译不安全代码”属性设置为true就可以了,方法如下:项目属性对话框->配置属性->生成->允许不安全代码块

namespace Pointer
{
    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  

            Console.ReadKey();
        }
    }  
}

  

C#查看各种变量的指针地址

标签:

原文地址:http://www.cnblogs.com/wj033/p/4617299.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!