zoukankan      html  css  js  c++  java
  • 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();
    }
  • 相关阅读:
    X 如何理解关系型数据库的常见设计范式?
    X 使用DMV,诊断和调优DB性能。
    X MSSQL-并发控制-2-Isolation msql 的各种隔离级别 sqlserver
    X SQL Server AG集群启动不起来的临时自救大招
    X 搭建非域AlwaysOn win2016+SQL2016
    X 从0开始搭建SQL Server AlwaysOn 第四篇(配置异地机房节点)
    X 从0开始搭建SQL Server AlwaysOn 第三篇(配置AlwaysOn)
    X 从0开始搭建SQL Server AlwaysOn 第二篇(配置故障转移集群)
    Python Cookie Session和分页
    Python django应用之corsheaders[跨域设置]
  • 原文地址:https://www.cnblogs.com/wangshide/p/2566469.html
Copyright © 2011-2022 走看看