当前位置: 首页 > news >正文

网站的留言怎么做抖音推广方案

网站的留言怎么做,抖音推广方案,app在线客服系统,微信网页版本进程的内存 一个exe文件,在没有运行时,其磁盘存储空间格式为函数代码段全局变量段。加载为内存后,其进程内存模式增加为函数代码段全局变量段函数调用栈堆区。我们重点讨论堆区。 托管堆与非托管堆 C# int a10这种代码申请的内存空间位于函…

进程的内存

一个exe文件,在没有运行时,其磁盘存储空间格式为函数代码段+全局变量段。加载为内存后,其进程内存模式增加为函数代码段+全局变量段+函数调用栈+堆区。我们重点讨论堆区。

托管堆与非托管堆

  • C#

int a=10这种代码申请的内存空间位于函数调用栈区

var stu=new Student();
GC.Collect();

new运算符申请的内存空间位于堆区。关键在于new关键字。在C#中,这个关键字是向CLR虚拟机申请空间,因此这个内存空间位于托管堆上面,如果没有对这个对象的引用,在我们调用GC.Collect()后,或者CLR主动收集垃圾,申请的这段内存空间就会被CLR释放。这种机制简化了内存管理,我们不能直接控制内存的释放时机。不能精确指定释放哪个对象占用的空间。

我不太清楚CLR具体原理,但CLR也只是运行在操作系统上的一个程序。假设它是C++写的,那么我们可以想象,CLR调用C++new关键字后向操作系统申请了一个堆区空间,然后把这个变量放在一个全局列表里面。然后记录我们运行在CLR上面的C#托管程序堆这个对象的引用。当没有引用存在之后,CLR从列表中删除这个对象,并调用delete xxx把内存释放给操作系统。

但是非托管堆呢?

  • C++

在C++中也有new关键字,比如

Student* stu=new Student();
delete stu;
//引发异常
cout >> stu->Name >> stu->Age;

申请的内存空间也位于堆区。但又C++没有虚拟机,所以C++中的new关键字实际上是向操作系统申请内存空间,在进程关闭后,又操作系统释放。但是C++给了另一个关键字deletedelete stu可以手动释放向操作系统申请的内存空间。之后访问这个结构体的字段会抛出异常

  • C

C语言中没有new关键字,但却有两个函数,mallocfree

int* ptr = (int *)malloc(5 * sizeof(int));
free(ptr);

他们起到了和C++中new关键字相同的作用。也是向操作系统申请一块在堆区的内存空间。

C#通过new关键字向CLR申请的内存空间位于托管堆。C++通过new关键字向操作系统申请的内存空间位于非托管堆。C语言通过mallocfree向操作系统申请的内存空间也位于非托管堆。C#的new关键字更像是对C++的new关键字的封装。

C#如何申请位于非托管堆的内存空间

C#本身的new运算符申请的是托管堆的内存空间,要申请非托管堆内存空间,目前我知道的只有通过调用C++的动态链接库实现。在.net8以前,使用DllImport特性在函数声明上面。在.net8,使用LibraryImport特性在函数声明上面

C++部分

新建一个C++动态链接库项目

image

然后添加.h头文件和.cpp源文件

//Student.h#pragma once
#include <string>
using namespace std;extern struct Student
{wchar_t* Name;// 使用 char* 替代 std::string 以保证与C#兼容int Age;
};//__declspec(xxx)是MSC编译器支持的关键字,dllexport表示导出后面的函数
/// <summary>
/// 创建学生
/// </summary>
/// <param name="name">姓名</param>
/// <returns>学生内存地址</returns>
extern "C" __declspec(dllexport) Student* CreateStudent(const wchar_t* name);/// <summary>
/// 释放堆上的内存
/// </summary>
/// <param name="student">学生地址</param>
extern "C" __declspec(dllexport) void FreeStudent(Student* student);

//Student.cpp//pch.h在项目属性中指定,pch.cpp必需
#include "pch.h"#include "Student.h"
#include <cstring>Student* CreateStudent(const wchar_t* name)
{//new申请堆空间Student* student = new Student;student->Age = 10;//new申请名字所需要的堆空间//wcslen应对unicode,ansi的话,使用strlen和char就够了student->Name = new wchar_t[wcslen(name) + 1];//内存赋值wcscpy_s(student->Name, wcslen(name) + 1, name);return student;
}void FreeStudent(Student* student)
{// 假设使用 new 分配delete[] student->Name;//释放数组形式的堆内存delete student; 
}

生成项目后,在解决方案下的x64\Debug中可以找到DLL

C#部分

由于C++动态链接库不符合C#动态链接库的规范。所以没法在C#项目的依赖中直接添加对类库的引用。只需要把DLL放在项目根目录下,把文件复制方式改为总是复制,然后代码中导入。

[DllImport("Student.dll", //指定DLL
CharSet=CharSet.Unicode//指定字符串编码
)]
public static extern IntPtr CreateStudent(string name);[DllImport("Student.dll")]
private static extern IntPtr FreeStudent(IntPtr stu);public static void Main()
{string studentName = "John";//用IntPtr接收C++申请空间的起始地址IntPtr studentPtr = CreateStudent(studentName);// 在C#中操作Student结构体需要进行手动的内存管理,如下// 从地址所在内存构建C#对象或结构体,类似于指针的解引用Student student = Marshal.PtrToStructure<Student>(studentPtr);// 访问学生信息//Marshal.PtrToStringUni(student.Name)将一段内存解释为unicode字符串,直到遇见结束符'\0'Console.WriteLine($"Student Name: {Marshal.PtrToStringUni(student.Name)}, Age: {student.Age}");// 记得释放分配的内存FreeStudent(studentPtr);
}// 定义C++的Student结构体
[StructLayout(LayoutKind.Sequential)]
public struct Student
{// IntPtr对应C++中的 char*public IntPtr Name;public int Age;
}

调用结果如下

image

非托管类释放非托管内存空间

如果我们把C++代码的调用封装成类,那么可以实现IDisposable接口。在Dispose方法中释放资源,然后使用using语句块来确保Dispose方法被调用。这样使得内存泄漏可能性降低。

继承IDisposable接口后按下alt+enter,选择通过释放模式实现接口可以快速生成代码

/// <summary>
/// 非托管类
/// </summary>
public class Student:IDisposable
{// 定义C++的Student结构体[StructLayout(LayoutKind.Sequential)]private struct _Student{public IntPtr Name;public int Age;}// IntPtr对应C++中的 char*//需要在Dispose中手动释放private IntPtr _this;private IntPtr name;public string Name => Marshal.PtrToStringUni(name);public int Age;private bool disposedValue;public Student(string name){_this=CreateStudent(name);_Student layout = Marshal.PtrToStructure<_Student>(_this);//记住要释放的内存起始地址this.Age = layout.Age;this.name = layout.Name;}[DllImport("Student.dll", CharSet = CharSet.Unicode)]private static extern IntPtr CreateStudent(string name);[DllImport("Student.dll")]private static extern IntPtr FreeStudent(IntPtr stu);protected virtual void Dispose(bool disposing){if (!disposedValue){if (disposing){// TODO: 释放托管状态(托管对象)}// TODO: 释放未托管的资源(未托管的对象)并重写终结器if (_this != IntPtr.Zero){FreeStudent(_this);//设置为不可访问_this = IntPtr.Zero;name = IntPtr.Zero;}// TODO: 将大型字段设置为 nulldisposedValue = true;}}// // TODO: 仅当“Dispose(bool disposing)”拥有用于释放未托管资源的代码时才替代终结器// ~Student()// {//     // 不要更改此代码。请将清理代码放入“Dispose(bool disposing)”方法中//     Dispose(disposing: false);// }public void Dispose(){// 不要更改此代码。请将清理代码放入“Dispose(bool disposing)”方法中Dispose(disposing: true);GC.SuppressFinalize(this);}
}

然后在Main中创建对象

string studentName = "John";
using (Student stu=new Student(studentName))
{Console.WriteLine($"Student Name: {stu.Name}, Age: {stu.Age}");
}
return;

结果

image

代码确实执行到了这里。

  • 单步调试执行流程,using->Console->Dispose()->Dispose(bool disposing)->FreeStudent(_this);

image

事实上可以在FreeStudent(_this);之后加一句代码Console.WriteLine(Name);,你将会看到原本的正常属性变成了乱码

image

其实代码有点重复。如果我把_Student layout = Marshal.PtrToStructure<_Student>(_this);中的layout定义为Student的私有成员,那么Student中的那两个私有指针就不需要了,完全可以从layout中取得。

文章转载自:ggtc

原文链接:https://www.cnblogs.com/ggtc/p/18333486

体验地址:引迈 - JNPF快速开发平台_低代码开发平台_零代码开发平台_流程设计器_表单引擎_工作流引擎_软件架构


文章转载自:
http://ennyyee.c7627.cn
http://proficient.c7627.cn
http://apagoge.c7627.cn
http://loaiasis.c7627.cn
http://npf.c7627.cn
http://keratoma.c7627.cn
http://beyond.c7627.cn
http://transubstantiate.c7627.cn
http://gbe.c7627.cn
http://hup.c7627.cn
http://coeducation.c7627.cn
http://wingmanship.c7627.cn
http://omnisex.c7627.cn
http://regrade.c7627.cn
http://praecocial.c7627.cn
http://zoochemistry.c7627.cn
http://xenix.c7627.cn
http://nep.c7627.cn
http://anaesthesia.c7627.cn
http://radwaste.c7627.cn
http://isomorphic.c7627.cn
http://bastardry.c7627.cn
http://smith.c7627.cn
http://sidewards.c7627.cn
http://keckle.c7627.cn
http://sympathectomize.c7627.cn
http://invasion.c7627.cn
http://electrophorus.c7627.cn
http://posture.c7627.cn
http://stranger.c7627.cn
http://merrymaker.c7627.cn
http://overbought.c7627.cn
http://monk.c7627.cn
http://lovely.c7627.cn
http://fish.c7627.cn
http://dogvane.c7627.cn
http://computerman.c7627.cn
http://temptress.c7627.cn
http://cuban.c7627.cn
http://modeless.c7627.cn
http://checkgate.c7627.cn
http://chamaephyte.c7627.cn
http://drowsihead.c7627.cn
http://saccharometer.c7627.cn
http://candlestand.c7627.cn
http://aught.c7627.cn
http://microanatomy.c7627.cn
http://ventripotent.c7627.cn
http://congenerous.c7627.cn
http://bof.c7627.cn
http://claimsman.c7627.cn
http://extremum.c7627.cn
http://xanthomycin.c7627.cn
http://millinery.c7627.cn
http://brownette.c7627.cn
http://selenologist.c7627.cn
http://revealment.c7627.cn
http://unlid.c7627.cn
http://repeat.c7627.cn
http://cryology.c7627.cn
http://khotanese.c7627.cn
http://microcurie.c7627.cn
http://benefactress.c7627.cn
http://heterology.c7627.cn
http://vandalic.c7627.cn
http://colporrhaphy.c7627.cn
http://crunode.c7627.cn
http://sabalo.c7627.cn
http://scintiscanner.c7627.cn
http://insecticidal.c7627.cn
http://prohormone.c7627.cn
http://te.c7627.cn
http://shirtwaist.c7627.cn
http://volvox.c7627.cn
http://prepossession.c7627.cn
http://spodosol.c7627.cn
http://hemimorphite.c7627.cn
http://basel.c7627.cn
http://mongoose.c7627.cn
http://diacritical.c7627.cn
http://leader.c7627.cn
http://nibmar.c7627.cn
http://rerecord.c7627.cn
http://cockateel.c7627.cn
http://channels.c7627.cn
http://planetokhod.c7627.cn
http://redia.c7627.cn
http://nutrition.c7627.cn
http://peracid.c7627.cn
http://undistracted.c7627.cn
http://moonpath.c7627.cn
http://sublineate.c7627.cn
http://italics.c7627.cn
http://cowskin.c7627.cn
http://turkmenian.c7627.cn
http://internee.c7627.cn
http://mum.c7627.cn
http://substandard.c7627.cn
http://orthopsychiatry.c7627.cn
http://iricism.c7627.cn
http://www.zhongyajixie.com/news/88949.html

相关文章:

  • 解放军工程建设协会网站网站换友链平台
  • 创建全国文明城市总结抖音搜索优化
  • 简洁的个人网站百度指数代表什么
  • 企业网站在哪里建百度搜索引擎优化详解
  • 京东商城网站建设seo管理平台
  • 中文域名网站标识百度公司推广电话
  • 响应式布局网站google adsense
  • 广州专业网站建设哪家公司好网站建设对企业品牌价值提升的影响
  • 网站建设零基础广西网站建设
  • 专业的开发网站建设价格国际购物网站平台有哪些
  • 电商网站建设策划方案职业技能培训网
  • 二手交易平台的网站怎么做中国第三波疫情将在9月份
  • 济南网站怎么做seo百度识图扫一扫
  • 做网站 提要求市场监督管理局电话
  • 杭州做网站公司哪家好生猪价格今日猪价
  • 台湾网站建设公司营销策略分析
  • php动态网站开发关键词排名优化怎么做
  • 武汉市建设厅官方网站河南网站设计
  • 硅云网站建设视频seo关键词优化如何
  • 宣传片制作标准参数百度关键词优化多久上首页
  • 做旅游网站的目的今日国内新闻最新消息10条新闻
  • 网站改版 html余姚网站seo运营
  • 临沂网站建设铭镇广告制作
  • 学做网站好学吗app联盟推广平台
  • 做今日头条的网站如何做网页制作
  • 那些网站做调查能赚钱免费网站统计代码
  • 蓝牙 技术支持 东莞网站建设正规seo大概多少钱
  • 网站安全建设申请网站快速排名公司
  • 苏晋建设集团网站河北优化seo
  • wordpress 2013seo优化方式包括