.net “托管”和“非托管”之间的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3563870/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between "managed" and "unmanaged"
提问by Louis Rhys
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. What are their difference, by definition? What are the consequences of using either of them? Does this distinction exist in .NET/Windows only?
我在谈论 .NET 时有时会听到/读到它,例如“托管代码”和“非托管代码”,但我不知道它们是什么以及它们的区别是什么。根据定义,它们有什么区别?使用其中任何一个的后果是什么?这种区别是否仅存在于 .NET/Windows 中?
回答by kurige
Managed Code
托管代码
Managed code is what Visual Basic .NET and C# compilers create. It runs on the CLR (Common Language Runtime), which, among other things, offers services like garbage collection, run-time type checking, and reference checking. So, think of it as, "My code is managedby the CLR."
托管代码是 Visual Basic .NET 和 C# 编译器创建的。它在 CLR(公共语言运行时)上运行,它提供垃圾收集、运行时类型检查和引用检查等服务。因此,可以将其视为“我的代码由 CLR管理”。
Visual Basic and C# can onlyproduce managed code, so, if you're writing an application in one of those languages you are writing an application managed by the CLR. If you are writing an application in Visual C++ .NET you can produce managed code if you like, but it's optional.
Visual Basic 和 C#只能生成托管代码,因此,如果您使用其中一种语言编写应用程序,则您正在编写由 CLR 管理的应用程序。如果您使用 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'. Also, since it compiles to machine code and not an intermediate language it is non-portable.
非托管代码直接编译为机器代码。因此,根据该定义,传统 C/C++ 编译器编译的所有代码都是“非托管代码”。此外,由于它编译为机器代码而不是中间语言,因此它是不可移植的。
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. 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.
由于 Visual C++ 可以编译为托管或非托管代码,因此可以在同一个应用程序中混合使用这两种代码。这模糊了两者之间的界限并使定义复杂化,但值得一提的是,您知道如果您使用第三方库以及一些编写不当的非托管代码,您仍然可能会出现内存泄漏。
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;
}
回答by Darin Dimitrov
This is more general than .NET and 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 和 Windows 更通用。托管是一种环境,您可以在其中进行自动内存管理、垃圾收集、类型安全……非托管就是其他一切。例如,.NET 是托管环境,而 C/C++ 是非托管的。
回答by RobertPitt
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).
托管代码是 Microsoft 创造的一种区分,用于识别需要且仅在公共语言运行时虚拟机(产生字节码)的“管理”下执行的计算机程序代码。
http://en.wikipedia.org/wiki/Managed_code

