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();
    }
  • 相关阅读:
    iview table的render()函数基本的用法
    【整理】iview Tree数据格式问题,无限递归树处理数据
    【整理】用JSON-server模拟REST API
    【整理】解决vue不相关组件之间的数据传递----vuex的学习笔记,解决报错this.$store.commit is not a function
    【整理】 vue-cli 打包后显示favicon.ico小图标
    【整理】treeGrid 树形表格
    【整理】iview中刷新页面的时候更新导航菜单的active-name
    [整理] webpack+vuecli打包生成资源相对引用路径与背景图片的正确引用
    在.vue文件中让html代码自动补全的方法(支持vscode)
    解决VSCode中使用vetur插件格式化vue文件时,js代码会自动加上冒号和分号
  • 原文地址:https://www.cnblogs.com/wangshide/p/2566469.html
Copyright © 2011-2022 走看看