简体   繁体   English

“托管”和“非托管”之间的区别

[英]Difference between “managed” and “unmanaged”

I hear/read about it sometimes when talking about .NET, for example "managed code" and "unmanaged code" but I have no idea what they are and what are their differences. 在谈论.NET时,我有时会听到/读到它,例如“托管代码”和“非托管代码”,但我不知道它们是什么以及它们之间的区别是什么。 What are their difference, by definition? 根据定义,它们有什么区别? What are the consequences of using either of them? 使用它们中的任何一个会产生什么后果? Does this distinction exist in .NET/Windows only? 这种区别仅存在于.NET / Windows中吗?

Managed Code 托管代码

Managed code is what Visual Basic .NET and C# compilers create. 托管代码是Visual Basic .NET和C#编译器创建的代码。 It runs on the CLR (Common Language Runtime), which, among other things, offers services like garbage collection, run-time type checking, and reference checking. 它在CLR(公共语言运行时)上运行,其中包括垃圾收集,运行时类型检查和引用检查等服务。 So, think of it as, "My code is managed by the CLR." 因此,将其视为“我的代码由CLR 管理 ”。

Visual Basic and C# can only produce managed code, so, if you're writing an application in one of those languages you are writing an application managed by the CLR. Visual Basic和C# 只能生成托管代码,因此,如果您正在使用其中一种语言编写应用程序,那么您正在编写由CLR管理的应用程序。 If you are writing an application in Visual C++ .NET you can produce managed code if you like, but it's optional. 如果您在Visual C ++ .NET中编写应用程序,则可以根据需要生成托管代码,但它是可选的。

Unmanaged Code 非托管代码

Unmanaged code compiles straight to machine code. 非托管代码直接编译为机器代码。 So, by that definition all code compiled by traditional C/C++ compilers is 'unmanaged code'. 因此,根据该定义,传统C / C ++编译器编译的所有代码都是“非托管代码”。 Also, since it compiles to machine code and not an intermediate language it is non-portable. 此外,由于它编译为机器代码而不是中间语言,因此它是不可移植的。

No free memory management or anything else the CLR provides. 没有可用的内存管理或CLR提供的任何其他内容。

Since you cannot create unmanaged code with Visual Basic or C#, in Visual Studio all unmanaged code is written in C/C++. 由于无法使用Visual Basic或C#创建非托管代码,因此在Visual Studio中,所有非托管代码都是用C / C ++编写的。

Mixing the two 混合两者

Since Visual C++ can be compiled to either managed or unmanaged code it is possible to mix the two in the same application. 由于Visual C ++可以编译为托管代码或非托管代码,因此可以将两者混合在同一个应用程序中。 This blurs the line between the two and complicates the definition, but it's worth mentioning just so you know that you can still have memory leaks if, for example, you're using a third party library with some badly written unmanaged code. 这模糊了两者之间的界限并使定义复杂化,但值得一提的是,如果您使用第三方库和一些写得不好的非托管代码,那么您仍然可能会遇到内存泄漏。

Here's an example I found by googling : 这是我通过谷歌搜索找到的一个例子:

#using <mscorlib.dll>
using namespace System;

#include "stdio.h"

void ManagedFunction()
{
    printf("Hello, I'm managed in this section\n");
}

#pragma unmanaged
UnmanagedFunction()
{
    printf("Hello, I am unmanaged through the wonder of IJW!\n");
    ManagedFunction();
}

#pragma managed
int main()
{
    UnmanagedFunction();
    return 0;
}

This is more general than .NET and Windows. 这比.NET和Windows更通用。 Managed is an environment where you have automatic memory management, garbage collection, type safety, ... unmanaged is everything else. 托管是一个环境,你有自动内存管理,垃圾收集,类型安全,...非托管是其他一切。 So for example .NET is a managed environment and C/C++ is unmanaged. 因此,例如.NET是托管环境,C / C ++是不受管理的。

Managed code is a differentiation coined by Microsoft to identify computer program code that requires and will only execute under the "management" of a Common Language Runtime virtual machine (resulting in Bytecode). 托管代码是微软创造的一种差异化,用于识别需要并且仅在公共语言运行时虚拟机的“管理”下执行的计算机程序代码(导致字节码)。

http://en.wikipedia.org/wiki/Managed_code http://en.wikipedia.org/wiki/Managed_code

http://www.developer.com/net/cplus/article.php/2197621/Managed-Unmanaged-Native-What-Kind-of-Code-Is-This.htm http://www.developer.com/net/cplus/article.php/2197621/Managed-Unmanaged-Native-What-Kind-of-Code-Is-This.htm

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM