C#中结构和类成员指针的内存分配

 

C#中结构和类成员指针的内存分配

http://mgc.name/article.asp?id=600

通过结构和类成员指针的操作,观察内存分配情况:
1.结构、类成员是按顺序排列在内存中的;
2.通过指针操作可以方便的指向任意变量。

TestPointerExampleTwo.cs:
01. using System;
02.   
03. namespace Magci.Test.Pointers
04. {
05.     //结构
06.     internal struct CurrencyStruct
07.     {
08.         public long Dollars;
09.         public byte Cents;
10.   
11.         public override string ToString()
12.         {
13.             return "$" + Dollars + "." + Cents;
14.         }
15.     }
16.   
17.     //类
18.     internal class CurrencyClass
19.     {
20.         public long Dollars;
21.         public byte Cents;
22.   
23.         public override string ToString()
24.         {
25.             return "$" + Dollars + "." + Cents;
26.         }
27.     }
28.   
29.     public class TestPointerExampleTwo
30.     {
31.         public static unsafe void Main()
32.         {
33.             Console.WriteLine("Size of CurrencyStruct struct is " sizeof(CurrencyStruct));
34.             CurrencyStruct amount1, amount2;
35.             CurrencyStruct* pAmount = &amount1;
36.             long* pDollars = & (pAmount->Dollars);
37.             byte* pCents = & (pAmount->Cents);
38.   
39.             Console.WriteLine("Address of amount1 is [0x{0:X}]", (uint)&amount1);
40.             Console.WriteLine("Address of amount2 is [0x{0:X}]", (uint)&amount2);
41.             Console.WriteLine("Address of pAmount is [0x{0:X}]", (uint)&pAmount);
42.             Console.WriteLine("Address of pDollars is [0x{0:X}]", (uint)&pDollars);
43.             Console.WriteLine("Address of pCents is [0x{0:X}]", (uint)&pCents);
44.   
45.             pAmount->Dollars = 20;
46.             *pCents = 50;
47.               
48.             Console.WriteLine("amount1 contains [{0}]", amount1);
49.             //amount2存储在amount1后面的地址上,pAmount开始指向amount1,递减后就指向了amount2
50.             --pAmount;
51.             //amount2没有进行初始化,值是随机的;使用指针时会绕过很多通常的编译检查,因此指针算法是不安全的
52.             Console.WriteLine("amount2 has address [0x{0:X}] and contains [{1}]", (uint)pAmount, *pAmount);
53.   
54.             //将pCents指向amount2.Cents
55.             CurrencyStruct* pTempCurrency = (CurrencyStruct*)pCents;
56.             pCents = (byte*)(--pTempCurrency);
57.             Console.WriteLine("Address of pCents is now [0x{0:X}]", (uint)&pCents);
58.               
59.             Console.WriteLine("\nNow with classes");
60.             CurrencyClass amount3 = new CurrencyClass();
61.   
62.             fixed (long* pDollars2 = &(amount3.Dollars))
63.             fixed (byte* pCents2 = &(amount3.Cents))
64.             {
65.                 Console.WriteLine("amount3.Dollars has address [0x{0:X}]", (uint)pDollars2);
66.                 Console.WriteLine("amount3.Cents has address [0x{0:X}]", (uint)pCents2);
67.                 *pDollars2 = -100;
68.                 Console.WriteLine("amount3 contains [{0}]", amount3);
69.             }
70.         }
71.     }
72. }

转载于:https://www.cnblogs.com/pumax/archive/2010/09/23/1833230.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值