C# managed, unmanaged, unsafe 的比较

1. unsafe与unmanaged的区别

managed code是在CLR监管下运行的程序。以下任务由CLR来执行:管理对象内存,类型安全检测和冗余处理。从另一方面来说,unmanaged code也就是能由程序员直接进行内存操作的程序。而unsafe是介于managed和unmanaged之间的桥梁,它使得managed code也能使用指针来控制和操作内存。

2. managed, unmanaged

Unmanaged code is the good old C++ with no CLR support, therefore unmanaged code does not have a garbage collector and you will have to keep track of all your memory allocations to avoid memory leaks. Also when you build an unmanaged project in Visual Studio, the resulting library or executable is written directly on machine code, therefore it doesn't need the .NET Framework to run.

The managed C++ has CLR support and its code is written in an extension of the C++ language called C++/CLI, this extension allows you to use the Garbage Collector and the .NET Framework classes.

Also when you build a managed project in Visual Studio, the resulting library or executable is written in CLR code (which is then translated to machine code by the CLR), therefore it needs the .NET Framework to run.

Usually you can choose to write managed or unmanaged C++ code when you create a Visual Studio project, but you can also add or remove CLR support from your project whenever you wish.

Finally if you choose to use unmanaged code (and are using Visual Studio 2005 or above), keep in mind that when you distribute your application you will also need to include the Visual C++ Redistributable Package, otherwise your application will not work (this is not required if you intend to run your application on a computer that already has Visual Studio installed).

3. Enable function-level control for compiling functions as managed or unmanaged.

#pragma managed
#pragma unmanaged
#pragma managed([push,] on | off)
#pragma managed(pop)

4. Remarks

The /clr compiler option provides module-level control for compiling functions either as managed or unmanaged.

An unmanaged function will be compiled for the native platform, and execution of that portion of the program will be passed to the native platform by the common language runtime.

Functions are compiled as managed by default when /clr is used.

Use the following guidelines when applying these pragmas:

Add the pragma preceding a function but not within a function body.

Add the pragma after #include statements (do not use these pragmas before #include statements).

The compiler ignores the managed and unmanaged pragmas if /clr is not used in the compilation.

When a template function is instantiated, the pragma state at the time of definition for the template determines if it is managed or unmanaged.

For more information, see Initialization of Mixed Assemblies.

5. Example

 托管、非托管的混合代码

1. 新建C++  CLR新项目

2. 更改项目属性(公共语言运行时支持 (/clr))

// pragma_directives_managed_unmanaged.cpp
// compile with: /clr
#include <stdio.h>

// func1 is managed
void func1() {
   System::Console::WriteLine("In managed function.");
}

// #pragma unmanaged
// push managed state on to stack and set unmanaged state
#pragma managed(push, off)

// func2 is unmanaged
void func2() {
   printf("In unmanaged function.\n");
}

// #pragma managed
#pragma managed(pop)
using namespace System;
// main is managed
int main() 
{
   func1();
   func2();
   Console::ReadKey();
}
posted @ 2012-06-27 22:49  Let it be!  阅读(3037)  评论(0编辑  收藏  举报