Esempio n. 1
0
        private static unsafe void Memory()
        {
            Console.WriteLine($"size of {nameof(MyEmptyStruct)} is: {sizeof(MyEmptyStruct)}, \n" +
                              $"size of {nameof(MyMemberStruct)} is: {sizeof(MyMemberStruct)}, \n" +
                              $"size of {nameof(MyMethodStruct)} is: {sizeof(MyMethodStruct)}, \n" +
                              $"size of {nameof(MyAutoStruct)} is: {sizeof(MyAutoStruct)}, \n" +
                              $"size of {nameof(MyExplicitStruct)} is: {sizeof(MyExplicitStruct)}, \n"
                              );

            MyMemberStruct s1 = new MyMemberStruct(), s2 = new MyMemberStruct(), s3 = Make(1);

            Console.WriteLine($"s1 = {s1}, s2 = {s2}, s3 = {s3}");
        }
Esempio n. 2
0
        // requires csc unsafe
        private static unsafe void Pointer()
        {
            Console.WriteLine($"#0 sizeof(int*)={sizeof(int*)}, sizeof(byte*)={sizeof(byte*)}");
            /* stack top - 0 */
            int var1 = 1000;                     // 4 bytes int 32 bits
            /* stack top - 1 */
            int var2 = 2000;                     // 4 bytes int 32 bits
            /* stack top - 4 */
            int *ptr = &var1;                    // 8 bytes pointer (int*) 32 bits
            /* stack top -5, top - 6 */
            var s1 = new MyMemberStruct(13, 17); // 8 bytes struct (two 32-bit integers) 64 bits
            /* stack top - 7 */
            int it = 0;                          // 4 bytes int 32 bits

            Console.WriteLine($"#1 var1 address:{(ulong)&var1}, value: {var1}; var2 address:{(ulong)&var2}, value: {var2}; s1: {s1}, address: {(ulong)&s1}");
            byte *memptr = null;

            //Change(ref var1);
            Console.WriteLine($"#2 var1 address:{(ulong)&var1}, value: {var1}, var2 address:{(ulong)&var2}, value: {var2}, address of pointer: {(ulong)&ptr}");

            Console.WriteLine("#3 for each integer in stack");
            //*(ptr - 1) = 1024; // ptr is var1 pointer, change var2 by ptr - 1
            for (it = 0; it < SIZE; it++)
            {
                Console.WriteLine($"  #3 address(ptr value, offset -{it}): { (ulong)(ptr - it)}, value: {(*(ptr - it))}");
            }
            Console.WriteLine("#4 for each byte in stack");
            memptr = (byte *)ptr;
            for (it = -3; it < SIZE * 4 - 2; it++)
            {
                Console.WriteLine($"  #4 address(ptr value, offset -{it}): { (ulong)(memptr - it)}, value: {*(memptr - it)}");
            }

            /* Stack不连续 */
            byte *array = stackalloc byte[4] {
                3, 5, 7, 11
            };                                                // 4 bytes

            Console.WriteLine($"#5 bytes array: {Show(array)}, array address: {(ulong)array}");

            *(ptr - ((ulong)ptr - (ulong)array) / 4) = 13;
            Console.WriteLine($"#6 bytes array: {Show(array)}, array address: {(ulong)array}"); // 13 0 0 0
            *(ptr - ((ulong)ptr - (ulong)array) / 4) = 0x13_14_15_16;
            Console.WriteLine($"#7 bytes array: {Show(array)}, array address: {(ulong)array}"); // 22(0x13) 21(0x14) 20(0x14) 19(0x13)
            *(byte *)(ptr - ((ulong)ptr - (ulong)array) / 4) = (byte)17;
            Console.WriteLine($"#8 bytes array: {Show(array)}, array address: {(ulong)array}"); // 17  21       20       19

            // 还有些东西没搞懂
        }