C#查看各種變數的指標地址

來源:互聯網
上載者:User

標籤:

FIXED介紹

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        }    }}

輸出結果:

C#查看各種變數的指標地址

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.