首页 > 系统相关 >C#通过不安全代码看内存加载

C#通过不安全代码看内存加载

时间:2022-12-09 19:35:11浏览次数:45  
标签:Console C# long v1 v2 v3 内存 WriteLine 加载

(注:本篇用点长,有点绕,耐心浏览)

  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的方式固定,不过代码量少的情况也不一定能触发回收。  

  想要更快更方便的了解相关知识,可以关注微信公众号 

 

 

标签:Console,C#,long,v1,v2,v3,内存,WriteLine,加载
From: https://www.cnblogs.com/axzxs2001/p/16969791.html

相关文章

  • ReferenceError和TypeError的区别
    ReferenceError:----指在RHS查询中所有嵌套的作用于中遍寻不到所需的变量,引擎就会抛出ReferenceError异常。ReferenceError同作用域判别失败相关,而TypeError这代表作用域......
  • qt msvc 静态库编译
    记录命令行编译过程:在下载的qtmsvcsource文件中拉起vsx86nativetoolscommand,输入configure-confirm-license-opensource-static-static-runtime-debug-an......
  • rsync远程同步
    一、rsync概述1.1rsync的概念rsync,remotesynchronize顾名思义就知道它是一款实现远程同步功能的软件,它在同步文件的同时,可以保持原来文件的权限、时间、软硬链接等附加......
  • vscode 的vim配置和快捷键配置
    keybinds.json//Placeyourkeybindingsinthisfiletooverridethedefaultsauto[][//以前配置的上下左右移动按键{"key":"alt+j","command":"......
  • System.CommandLine版CSRebot
    之前自己实现过一个CSRebot命令行工具,现在用System.CommandLine来实现,就规范和省事多了,虽然System.CommandLine还没有正式发布,但它的实现思路还是很不错的。下面的......
  • wlan_cli用法
    #启动脚本wpa_supplicant-iwlan0-Dnl80211-c/etc/ambaipcam/IPC_Q313/config/wlan/wpa_supplicant.conf-B#查询网卡状态wpa_cli-p/var/run/wpa_supplicant-iwlan0st......
  • System.CommandLine版CSRebot
    之前自己实现过一个CSRebot命令行工具,现在用System.CommandLine来实现,就规范和省事多了,虽然System.CommandLine还没有正式发布,但它的实现思路还是很不错的。下面......
  • C++基础
    1.任何合法的C程序都是C++程序。2.c++是一门面向对象的语言。3.标准的C++由三个重要部分组成:核心语言,提供了所有构件块,包括变量、数据类型和常量,等等。C++标准库,提......
  • Ctr 创建nginx容器并使用curl访问
    这篇文章使用ctr创建一个nginx容器,并在宿主机使用curl进行访问。Prerequisite按照ContainerdQuickstart完成:安装cni安装runc安装containerd,并以systemdservice......
  • echarts的option有哪些配置项
    正在思考,请等待......全局配置项:backgroundColor、textStyle、title、tooltip、legend、dataZoom、visualMap、toolbox、animation、parallel、parallelAxis、calendar、......