ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

C# managed, unmanaged, unsafe 的比较

2019-08-02 12:01:53  阅读:359  来源: 互联网

标签:function code managed C# unsafe pragma unmanaged CLR


原文链接:http://www.cnblogs.com/wangshide/archive/2012/06/27/2566469.html

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();
}

转载于:https://www.cnblogs.com/wangshide/archive/2012/06/27/2566469.html

标签:function,code,managed,C#,unsafe,pragma,unmanaged,CLR
来源: https://blog.csdn.net/weixin_30709635/article/details/98186960

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有