文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

C#通过不安全代码看内存加载的示例详解

2024-04-02 19:55

关注

C#中类型分为值类型和引用类型,值类型存储在堆栈中,是栈结构,先进后出,引用类型存储在托管堆中。接下来用不安全代码的地址,来看一下值类型和引用类型的存储。

项目文件

C#中使用不安全代码需要在项目文件中添加AllowUnsafeBlocks配置。

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net7.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
    <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
  </PropertyGroup>
</Project>

所有的测试案例都是定义两个特定类型的变量,然后查看它的内存地址,然后进行调用一个方法进行相加运算,然后分别在方法内输出变量和结查内存地址,最后返回主方法后变量的内存地址。

值类型

static void TestDouble()
{
    var v1 = 1.00001d;
    var v2 = 2.00002d;
    Console.WriteLine("TestDouble v1 " + (long)&v1);
    Console.WriteLine("TestDouble v2 " + (long)&v2);
    Console.WriteLine("TestDouble v2-v1 " + ((long)&v2 - (long)&v1));
    var v3 = Add(v1, v2);
    Console.WriteLine("TestDouble v3 " + (long)&v3);
    Console.WriteLine("TestDouble v3-v2 " + ((long)&v3 - (long)&v2));
    Console.WriteLine("TestDouble v3-v1 " + ((long)&v3 - (long)&v1));
}

static double Add(double v1, double v2)
{
    Console.WriteLine("Add v1 " + (long)&v1);
    Console.WriteLine("Add v2 " + (long)&v2);
    Console.WriteLine("Add v2-v1 " + ((long)&v2 - (long)&v1));
    var v3 = v1 + v2;
    Console.WriteLine("Add v3 " + (long)&v3);
    Console.WriteLine("Add v3-v2 " + ((long)&v3 - (long)&v2));
    Console.WriteLine("Add v3-v1 " + ((long)&v3 - (long)&v1));
    return v3;
}

v1的所在内存地址大于v2,最后运算完的v3是最小的,我们可以想象,v1放在栈的最后面,地址最大,然后放v2,最后放v3。回收时的顺序是反回来的。那么Add方法里,v2地址最大,但比TestDouble都要小,说明进栈要晚一些,接下来是v1进栈,最后是v3进栈,不过TestDouble里的每个变量都相差8,但方法里的就不是了,这是因为方法参数,返回值等信息,还要占一些内存空间。还有TestDouble的v3为什么能和v2相差8?不是有Add方法吗?原因是Add调用完后都出栈了,所以TestDouble的v3和v2是相邻的。

自定义结构体

struct TestStruct
{
    public TestStruct()
    {
        i = 100;
    }
    public long i;
}
static void TestTestStruct()
{
    var v1 = new TestStruct();
    Console.WriteLine("TestStruct原v1对象地址= " + (long)&v1);
    var v2 = new TestStruct();
    Console.WriteLine("TestStruct原v2对象地址= " + (long)&v2);
    Console.WriteLine("TestStruct v2-v1 " + ((long)&v2 - (long)&v1));
    var v3 = Add(v1, v2);
    Console.WriteLine("TestStruct原v3对象地址= " + (long)&v3);
    Console.WriteLine("TestStruct v3-v2 " + ((long)&v3 - (long)&v2));
}
static TestStruct Add(TestStruct v1, TestStruct v2)
{
    Console.WriteLine("Add TestStruct v1对象地址= " + (long)&v1);
    Console.WriteLine("Add TestStruct v2对象地址= " + (long)&v2);
    Console.WriteLine("Add TestStruct  v2-v1 " + ((long)&v2 - (long)&v1));
    var v3 = new TestStruct();
    v3.i = v1.i + v2.i;
    Console.WriteLine("Add TestStruct v3对象地址" + (long)&v3);
    Console.WriteLine("Add TestStruct  v3-v2 " + ((long)&v3 - (long)&v2));
    return v3;
}

自定义struct与double类似,本质上double也是用struct定义的。

引用类型string

static void TestString()
{
    long ad1, ad2, ad3;
    var v1 = "aaaa";
    var v2 = "bbbb";
    fixed (char* p = v1)
    {
        ad1 = (long)p;
        Console.WriteLine("TestString v1字符串地址= " + (long)p);
    }
    fixed (char* p = v2)
    {
        ad2 = (long)p;
        Console.WriteLine("TestString v2字符串地址= " + (long)p);
    }
    Console.WriteLine("TestString v2-v1 " + (ad2 - ad1));
    var v3 = Add(v1, v2);
    fixed (char* p = v3)
    {
        ad3 = (long)p;
        Console.WriteLine("TestString v3字符串地址= " + (long)p);
    }
    Console.WriteLine("TestString v3-v2 " + (ad3 - ad2));
}

static string Add(string v1, string v2)
{
    long ad1, ad2, ad3;
    fixed (char* p = v1)
    {
        ad1 = (long)p;
        Console.WriteLine("Add中v1字符串地址= " + (long)p);
    }
    fixed (char* p = v2)
    {
        ad2 = (long)p;
        Console.WriteLine("Add中v2字符串地址= " + (long)p);
    }
    Console.WriteLine("Add中 v2-v1 " + (ad2 - ad1));
    var v3 = v1 + v2;
    fixed (char* p = v3)
    {
        ad3 = (long)p;
        Console.WriteLine("Add中v3字符串地址= " + (long)p);
    }
    Console.WriteLine("Add中 v3-v2 " + (ad3 - ad2));
    Console.WriteLine("Add中 v3-v1 " + (ad3 - ad1));
    return v3;
}
static void TestString2()
{
    var v1 = "aaaa";
    var v2 = "bbbb";
    var h1 = GCHandle.Alloc(v1, GCHandleType.Pinned);
    Console.WriteLine("TestString2 v1对象地址= " + (long)h1.AddrOfPinnedObject());
    var h2 = GCHandle.Alloc(v2, GCHandleType.Pinned);
    Console.WriteLine("TestString2 v2对象地址= " + (long)h2.AddrOfPinnedObject());
    Console.WriteLine("TestString2 v2-v1 " + ((long)h2.AddrOfPinnedObject() - (long)h1.AddrOfPinnedObject()));
    var v3 = Add2(v1, v2);
    var h3 = GCHandle.Alloc(v3, GCHandleType.Pinned);
    Console.WriteLine("TestString2 v3对象地址= " + (long)h3.AddrOfPinnedObject());
    Console.WriteLine("TestString2 v3-v2 " + ((long)h3.AddrOfPinnedObject() - (long)h2.AddrOfPinnedObject()));
}
static string Add2(string v1, string v2)
{
    var h1 = GCHandle.Alloc(v1, GCHandleType.Pinned);
    Console.WriteLine("Add2中的v1对象地址= " + (long)h1.AddrOfPinnedObject());
    var h2 = GCHandle.Alloc(v2, GCHandleType.Pinned);
    Console.WriteLine("Add2中的v2对象地址= " + (long)h2.AddrOfPinnedObject());
    Console.WriteLine("Add2 v2-v1 " + ((long)h2.AddrOfPinnedObject() - (long)h1.AddrOfPinnedObject()));
    var v3 = v1 + v2;
    var h3 = GCHandle.Alloc(v3, GCHandleType.Pinned);
    Console.WriteLine("Add2中的v3对象地址= " + (long)h3.AddrOfPinnedObject());
    Console.WriteLine("Add2 v3-v2 " + ((long)h3.AddrOfPinnedObject() - (long)h2.AddrOfPinnedObject()));
    Console.WriteLine("Add2 v3-v1 " + ((long)h3.AddrOfPinnedObject() - (long)h1.AddrOfPinnedObject()));
    return v3;
}

字符串是引用类型,v1比v2内存地址小,进入Add后,v1和v2与传入的地址相同,因为是引用类型,Add方法里的v3接着往大走,并且与返回的v3是一个地址,这些没有问题。

string用了两种方法,发现两个方式v1都是aaaa,v2都是bbbb,因为字符串有留用性,所以两个方法的v1和v2是一样的;但两种方式调用了Add后,在Add里的v3都是aaaabbbb,都是拼接,但拼出来的字符串的地址不相同,所以这块没有留用。

自定class类型

class TestClass
{
     public int i = 100;
}
static void TestTestClass()
{
    var v1 = new TestClass();
    var h1 = GCHandle.Alloc(v1, GCHandleType.Pinned);
    Console.WriteLine("TestTestClass v1对象地址= " + (long)h1.AddrOfPinnedObject());
    var v2 = new TestClass();
    var h2 = GCHandle.Alloc(v2, GCHandleType.Pinned);
    Console.WriteLine("TestTestClass v2对象地址= " + (long)h2.AddrOfPinnedObject());
    Console.WriteLine("TestTestClass v2-v1 " + ((long)h2.AddrOfPinnedObject() - (long)h1.AddrOfPinnedObject()));
    var v3 = Add(v1, v2);
    var h3 = GCHandle.Alloc(v3, GCHandleType.Pinned);
    Console.WriteLine("TestTestClass 3对象地址= " + (long)h3.AddrOfPinnedObject());
    Console.WriteLine("TestTestClass v3-v2 " + ((long)h3.AddrOfPinnedObject() - (long)h2.AddrOfPinnedObject()));
}
static TestClass Add(TestClass v1, TestClass v2)
{
    var h1 = GCHandle.Alloc(v1, GCHandleType.Pinned);
    Console.WriteLine("Add中的v1对象地址= " + (long)h1.AddrOfPinnedObject());
    var h2 = GCHandle.Alloc(v2, GCHandleType.Pinned);
    Console.WriteLine("Add中的v2对象地址= " + (long)h2.AddrOfPinnedObject());
    Console.WriteLine("Add中 v2-v1 " + ((long)h2.AddrOfPinnedObject() - (long)h1.AddrOfPinnedObject()));
    var v3 = new TestClass();
    v3.i = v1.i + v2.i;
    var h3 = GCHandle.Alloc(v3, GCHandleType.Pinned);
    Console.WriteLine("Add中的v3对象地址= " + (long)h3.AddrOfPinnedObject());
    Console.WriteLine("Add中 v3-v2 " + ((long)h3.AddrOfPinnedObject() - (long)h2.AddrOfPinnedObject()));
    Console.WriteLine("Add中 v3-v1 " + ((long)h3.AddrOfPinnedObject() - (long)h1.AddrOfPinnedObject()));
    return v3;
}

自定义class,每次都是新地址,没有留用性,并且地址都是在增加。

当然引用类型的地址不是一成不变的,因为有垃圾回放,重新整理的过程,本例用用Pinned的方式固定,不过代码量少的情况也不一定能触发回收。

以上就是C#通过不安全代码看内存加载的示例详解的详细内容,更多关于C# 内存加载的资料请关注编程网其它相关文章!

阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     801人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     348人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     311人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     432人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     220人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-后端开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯