C # view the pointer addresses of various variables

Source: Internet
Author: User
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 {// static variables are stored on the stack, fixed fixed static int m_sz = 100 is required for viewing pointers; // common data members are also placed on the heap, and fixed int m_ndata = 100 is required for viewing pointers; // It is equivalent to the # define Statement of C/C ++. No memory const int Pi = 31415 is allocated; // unsafe can be placed in the main function declaration !! Static unsafe void main (string [] ARGs) {// place simple structure variables on the stack, without fixed XYZ stdata = new XYZ (); stdata. A = 100; console. writeline ("schema variable = 0x {0: x}", (INT) & stdata); // The Declaration of the array variable is placed on the stack, and the data is placed on the stack, use fixed to fix int [] arry = NULL; arry = new int [10]; fixed (int * P = arry) {console. writeline ("array = 0x {0: x}", (INT) P) ;}// these variables on the stack, you can directly direct the pointer to // from the data of the printed pointer. Int Is 4 bytes, double is 8 bytes int y = 10; int z = 100; double F = 0.90; conso Le. writeline ("local variable Y = 0x {0: x}, Z = 0x {1: x}", (INT) & Y, (INT) & Z); console. writeline ("local variable F = 0x {0: x}", (INT) & F); // The following failed // fixed (int * P = & P. pi) // {//} // address of the data in the heap, you must use the fixed statement! String Ss = "HELO"; fixed (char * P = SS) {console. writeline ("string address = 0x {0: x}", (INT) P) ;}program P = new program (); // This is a class object, put fixed (int * P = & P. m_ndata) {console. writeline ("common class member variable = 0x {0: x}", (INT) P);} // static member variable fixed (int * P = & m_sz) on the stack) {console. writeline ("static member variable = 0x {0: x}", (INT) P);} // The number of bytes occupied by each type is shown in the console. write ("\ n is the number of bytes occupied by each type \ 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 }}}
Output result:

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.