c#win中malloc的定义。形成 [英] Definition of malloc in c# win. form

查看:72
本文介绍了c#win中malloc的定义。形成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找不到 malloc 的定义和用法,有人可以通过我解释一下。示例

i cannot find definition and use of malloc can somebody explain it to me via. example

推荐答案

C#没有,或以任何方式,形状或形式使用malloc。



.NET应用程序是托管的:这意味着它们内置了自己的内存管理(通过 new 关键字)并绑定到垃圾收集系统。它们不使用malloc,也不用,因为系统会为你处理内存。
C# does not have, or use malloc in any way, shape or form.

.NET applications are Managed: which means they have their own memory management built in (via the new keyword) and which is tied into the Garbage Collection system. They do not use malloc, or free as the memory is handled by the system for you.


Griff是对的(大部分):)



99.999%的时间,使用.NET中的对象,你只能使用新操作符,它适用于托管内存。



但是,在极少数情况下,您需要分配非托管内存,就像使用malloc一样。 .NET确实为Marshal类提供了一种方法,特别是 Marshal .AllocHGlobal [ ^ ]和 Marshal .FreeHGlobal [ ^ ]。



值得注意的是,除非你是真的想要使用上述内容使用一些专门的非托管代码。分配内存并忘记释放它会造成内存泄漏。
Griff is right (mostly) :)

99.999% of the time, working with objects in .NET you will only use the "new" operator, which works with managed memory.

However, there are rare circumstances that you need to allocate unmanaged memory, just like you would with malloc. .NET does provide a method for doing this with the Marshal class, specifically the Marshal.AllocHGlobal[^] and Marshal.FreeHGlobal[^].

Its also worth noting that you don't really want to use the above unless you are working with some specialized unmanaged code. Allocating memory and forgetting to free it creates memory leaks.


这篇关于c#win中malloc的定义。形成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆