C# managed, unmanaged, unsafe 的比較

來源:互聯網
上載者:User
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. 更改項目屬性(Common Language Runtime 支援 (/clr))

// pragma_directives_managed_unmanaged.cpp// compile with: /clr#include <stdio.h>// func1 is managedvoid 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 unmanagedvoid func2() {   printf("In unmanaged function.\n");}// #pragma managed#pragma managed(pop)using namespace System;// main is managedint main() {   func1();   func2();   Console::ReadKey();}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.