Пример #1
0
        unsafe public static void Main()
        {
            Console.WriteLine($"Size of CurrencyStruct struct is {sizeof(CurrencyStruct)}");
            CurrencyStruct  amount1, amount2;
            CurrencyStruct *pAmount  = &amount1;
            long *          pDollars = &(pAmount->Dollars);
            byte *          pCents   = &(pAmount->Cents);

            Console.WriteLine($"Address of amount1 is 0x{(ulong)&amount1:X}");
            Console.WriteLine($"Address of amount2 is 0x{(ulong)&amount2:X}");
            Console.WriteLine($"Address of pAmount is 0x{(ulong)&pAmount:X}");
            Console.WriteLine($"Address of pDollars is 0x{(ulong)&pDollars:X}");
            Console.WriteLine($"Address of pCents is 0x{(ulong)&pCents:X}");
            pAmount->Dollars = 20;
            *pCents = 50;
            Console.WriteLine($"amount1 contains {amount1}");

            --pAmount;   // this should get it to point to amount2
            Console.WriteLine($"amount2 has address 0x{(ulong)pAmount:X} " +
                              $"and contains {*pAmount}");

            // do some clever casting to get pCents to point to cents
            // inside amount2
            CurrencyStruct *pTempCurrency = (CurrencyStruct *)pCents;

            pCents = (byte *)(--pTempCurrency);
            Console.WriteLine("Address of pCents is now 0x{0:X}", (ulong)&pCents);

            Console.WriteLine("\nNow with classes");
            // now try it out with classes
            var amount3 = new CurrencyClass();

            fixed(long *pDollars2 = &(amount3.Dollars))
            fixed(byte *pCents2 = &(amount3.Cents))
            {
                Console.WriteLine($"amount3.Dollars has address 0x{(ulong)pDollars2:X}");
                Console.WriteLine($"amount3.Cents has address 0x{(ulong)pCents2:X}");
                *pDollars2 = -100;
                *pCents2   = 2;
                Console.WriteLine($"amount3 contains {amount3}");
                var d = &pDollars2;

                //&pCents2;
                Console.WriteLine($"amount3.Dollars has address 0x{(ulong)pDollars2:X}");
                Console.WriteLine($"amount3.Cents has address 0x{(ulong)pCents2:X}");
            }
        }
        unsafe public static void Main()
        {
            WriteLine($"Size of CurrencyStruct struct is {sizeof(CurrencyStruct)}");
            CurrencyStruct amount1, amount2;
            CurrencyStruct* pAmount = &amount1;
            long* pDollars = &(pAmount->Dollars);
            byte* pCents = &(pAmount->Cents);

            WriteLine($"Address of amount1 is 0x{(ulong)&amount1:X}");
            WriteLine($"Address of amount2 is 0x{(ulong)&amount2:X}");
            WriteLine($"Address of pAmount is 0x{(ulong)&pAmount:X}");
            WriteLine($"Address of pDollars is 0x{(ulong)&pDollars:X}");
            WriteLine($"Address of pCents is 0x{(ulong)&pCents:X}");
            pAmount->Dollars = 20;
            *pCents = 50;
            WriteLine($"amount1 contains {amount1}");

            --pAmount;   // this should get it to point to amount2
            WriteLine($"amount2 has address 0x{(ulong)pAmount:X} " +
                $"and contains {*pAmount}");

            // do some clever casting to get pCents to point to cents
            // inside amount2
            CurrencyStruct* pTempCurrency = (CurrencyStruct*)pCents;
            pCents = (byte*)(--pTempCurrency);
            WriteLine("Address of pCents is now 0x{0:X}", (ulong)&pCents);

            WriteLine("\nNow with classes");
            // now try it out with classes
            var amount3 = new CurrencyClass();

            fixed (long* pDollars2 = &(amount3.Dollars))
            fixed (byte* pCents2 = &(amount3.Cents))
            {
                WriteLine($"amount3.Dollars has address 0x{(ulong)pDollars2:X}");
                WriteLine($"amount3.Cents has address 0x{(ulong)pCents2:X}");
                *pDollars2 = -100;
                WriteLine($"amount3 contains {amount3}");
            }
        }
Пример #3
0
        private static unsafe void Main()
        {
            Console.WriteLine("Size of Currency struct is " + sizeof(CurrencyStruct));
            CurrencyStruct  amount1, amount2;
            CurrencyStruct *pAmount  = &amount1;
            long *          pDollars = &(pAmount->Dollars);
            byte *          pCents   = &(pAmount->Cents);

            Console.WriteLine("Address of amount1 is 0x{0:X}", (uint)&amount1);
            Console.WriteLine("Address of amount2 is 0x{0:X}", (uint)&amount2);
            Console.WriteLine("Address of pAmt is 0x{0:X}", (uint)&pAmount);
            Console.WriteLine("Address of pDollars is 0x{0:X}", (uint)&pDollars);
            Console.WriteLine("Address of pCents is 0x{0:X}", (uint)&pCents);
            pAmount->Dollars = 20;
            *pCents = 50;
            Console.WriteLine("amount1 contains " + amount1);
            --pAmount;   // this should get it to point to amount2
            Console.WriteLine("amount2 has address 0x{0:X} and contains {1}",
                              (uint)pAmount, *pAmount);
            // do some clever casting to get pCents to point to cents
            // inside amount2
            CurrencyStruct *pTempCurrency = (CurrencyStruct *)pCents;

            pCents = (byte *)(--pTempCurrency);
            Console.WriteLine("Address of pCents is now 0x{0:X}", (uint)&pCents);
            Console.WriteLine("\nNow with classes");
            // now try it out with classes
            CurrencyClass amount3 = new CurrencyClass();

            fixed(long *pDollars2 = &(amount3.Dollars))
            fixed(byte *pCents2 = &(amount3.Cents))
            {
                Console.WriteLine("amount3.Dollars has address 0x{0:X}", (uint)pDollars2);
                Console.WriteLine("amount3.Cents has address 0x{0:X}", (uint)pCents2);
                *pDollars2 = -100;
                Console.WriteLine("amount3 contains " + amount3);
            }
            Console.ReadLine();
        }
Пример #4
0
        private static unsafe void Main()
        {
            Console.WriteLine("Size of Currency struct is {0}", sizeof(CurrencyStruct));
            CurrencyStruct  amount1, amount2;
            CurrencyStruct *pAmount  = &amount1;
            long *          pDollars = &(pAmount->Dollars);
            byte *          pCents   = &(pAmount->Cents);

            Console.WriteLine("Address of amount1 is 0x{0:X}", (uint)&amount1);
            Console.WriteLine("Address of amount2 is 0x{0:X}", (uint)&amount2);
            Console.WriteLine("Address of pAmt is 0x{0:X}", (uint)&pAmount);
            Console.WriteLine("Address of pDollars is 0x{0:X}", (uint)&pDollars);
            Console.WriteLine("Address of pCents is 0x{0:X}", (uint)&pCents);
            pAmount->Dollars = 20;
            *pCents = 50;
            Console.WriteLine("amount1 contains {0}", amount1);
            --pAmount; // Note: это должно переустановить указатель на amount2
            Console.WriteLine("amount2 has address 0x{0:X} and contains {1}", (uint)pAmount, *pAmount);
            // Note: Выполнить некоторое интеллектуальное приведение, чтобы установить
            // указатель pCents на значение центов внутри amount2
            var pTempCurrency = (CurrencyStruct *)pCents;

            pCents = (byte *)(--pTempCurrency);
            Console.WriteLine("Address of pCents is now 0x{0:X}", (uint)&pCents);
            Console.WriteLine("\nNow with classes");
            // Пробуем указатели с классами
            var amount3 = new CurrencyClass();

            fixed(long *pDollars2 = &(amount3.Dollars))
            fixed(byte *pCents2 = &(amount3.Cents))
            {
                Console.WriteLine("amount3.Dollars has address 0x{0:X}", (uint)pDollars2);
                Console.WriteLine("amount3.Cents has address 0x{0:X}", (uint)pCents2);
                *pDollars2 = -100;
                Console.WriteLine("amount3 contains {0}", amount3);
            }

            Console.ReadLine();
        }