C # comparison of managed, unmanaged, and unsafe

Source: Internet
Author: User
1. Differences between unsafe and unmanaged

The managed code runs under CLR supervision.Program. The following tasks are executed by CLR: Object Memory Management, type security detection, and redundancy processing. On the other hand, the unmanaged code is a program that allows programmers to directly perform memory operations. Unsafe is a bridge between managed and unmanaged, which enables the managed code to use pointers to control and operate on memory.

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. 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.. 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. 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

Managed and unmanaged HybridCode

1. Create a new C ++ CLR Project

2. Change Project Properties (supported by the Common Language Runtime (/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 ();} 
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.