You must Sign In to post a response.
  • Category: .NET

    difference between managed and unmanaged code ?

    The forum thread has not been reviewed by Editors yet. Readers are advised to use their best judgement before accessing this thread.
    This thread will be reviewed shortly.
    If you think this thread contain inappropriate content, please report to webmaster.
    what is the difference between managed code and unmanaged code And also differece between managed data and unmanaged data ?
  • #16795
    Managed Code:
    Code that runs under a "contract of cooperation" with the common language runtime. Managed code must supply the metadata necessary for the runtime to provide services such as memory management, cross-language integration, code access security, and
    automatic lifetime control of objects. All code based on Microsoft intermediate language (MSIL) executes as managed code.

    Un-Managed Code:
    Code that is created without regard for the conventions and requirements of the common language runtime. Unmanaged code executes in the common language runtime environment with minimal services (for example, no garbage collection, limited debugging, and so on).

  • #16798
    Your first answer is in following link -

    I think the Data which are not collected by the garbage collector are Unmanaged Data and a good example of it is the Pointers of C#.


  • #16805
    Managed Code:-
    Managed code is code written in one of over twenty high-level programming languages that are available for use with the Microsoft® .NET Framework, including C#, J#, Microsoft Visual Basic® .NET, Microsoft JScript® .NET, and C++. All of these languages share a unified set of class libraries and can be encoded into an Intermediate Language (IL). A runtime-aware compiler compiles the IL into native executable code within a managed execution environment that ensures type safety, array bound and index checking, exception handling, and garbage collection.

    By using managed code and compiling in this managed execution environment, you can avoid many typical programming mistakes that lead to security holes and unstable applications. Also, many unproductive programming tasks are automatically taken care of, such as type safety checking, memory management, and destruction of unneeded objects. You can therefore focus on the business logic of your applications and write them using fewer lines of code. The result is shorter development time and more secure and stable applications.

    UnManaged Code
    The Microsoft .NET Framework promotes interaction with COM components, COM+ services, external type libraries, and many operating system services. Data types, method signatures, and error-handling mechanisms vary between managed and unmanaged object models. To simplify interoperation between .NET Framework components and unmanaged code and to ease the migration path, the common language runtime conceals from both clients and servers the differences in these object models.

    Code executing under the control of the runtime is called managed code. Conversely, code that runs outside the runtime is called unmanaged code. COM components, ActiveX interfaces, and Win32 API functions are examples of unmanaged code.

  • #16808
    check out this link..for more clear CUT.

    Thanks -- Vijaya Kadiyala
    Microsoft MVP

  • #16829

    Check this


  • #89921
    Managed Code

    Managed code is code that is written to target the services of the managed runtime execution environment (like Common Language Runtime in .NET Framework). The managed code is always executed by a managed runtime execution environment rather than the operating system directly. Managed refers to a method of exchanging information between the program and the runtime environment. Because the execution of code is governed by the runtime environment, the environment can guarantee what the code is going to do and provide the necessary security checks before executing any piece of code. Because of the same reason the managed code also gets different services from the runtime environment like Garbage Collection, type checking, exception handling, bounds checking, etc. This way managed code does not have to worry about memory allocations, type safety, etc. Applications written in Java, C#, VB.NET, etc target a runtime environment which manages the execution and the code written using these types of languages is known as Managed Code. Managed code is always compiled into an Intermediate Language (MSIL in case of .NET Framework). The compiler used by .NET framework to compile managed code compiles it into Intermediate Language and generates the necessary metadata, symbolic information that describes all of the entry points and the constructs exposed in the Intermediate Language (e.g., methods, properties) and their characteristics. The Common Language Infrastructure (CLI) Standard describes how the information is to be encoded, and programming languages that target the runtime emit the correct encoding.

    In .NET Framework Managed Code runs within the .Net Framework's CLR and benefits from the services provided by the CLR. When we compile the managed code, the code gets compiled to an intermediate language (MSIL) and an executable is created. When a user runs the executable the Just In Time Compiler of CLR compiles the intermediate language into native code specific to the underlying architecture. Since this translation happens by the managed execution environment (CLR), the managed execution environment can make guarantees about what the code is going to do, because it can actually reason about it. It can insert traps and sort of protection around, if it's running in a sandboxed environment, it can insert all the appropriate garbage collection hooks, exception handling, type safety, array bounce, index checking and so forth.

    Managed code also provides platform independence. As the managed code is first compiled to intermediate language, the CLR's JIT Compiler takes care of compiling this intermediate language into the architecture specific instructions.

    Unmanaged Code

    Code that is directly executed by the Operating System is known as un-managed code. Typically applications written in VB 6.0, C++, C, etc are all examples of unmanaged code. Unmanaged code typically targets the processor architecture and is always dependent on the computer architecture. Unmanaged code is always compiled to target a specific architecture and will only run on the intended platform. This means that if you want to run the same code on different architecture then you will have to recompile the code using that particular architecture. Unmanaged code is always compiled to the native code which is architecture specific. When we compile unmanaged code it gets compiled into a binary X86 image. And this image always depends on the platform on which the code was compiled and cannot be executed on the other platforms that are different that the one on which the code was compiled. Unmanaged code does not get any services from the managed execution environment.

    In unmanaged code the memory allocation, type safety, security, etc needs to be taken care of by the developer. This makes unmanaged code prone to memory leaks like buffer overruns and pointer overrides and so forth.

    Unmanaged executable files are basically a binary image, x86 code, loaded into memory. The program counter gets put there and that's the last the Operating System knows. There are protections in place around memory management and port I/O and so forth, but the system doesn't actually know what the application is doing.

  • #184435
    Microsoft Visual Studio

    .NET (VS.NET) is capable of producing both managed and unmanaged code. Basically
    unmanaged code is the kind that runs directly against the Win32 API and the x86
    instruction set, the kind we're used to. Managed code runs on .NET's CLR (common
    language runtime), the equivalent of Java's VM.

    Managed code comes in
    two forms, safe and unsafe. Safe code only accesses memory that's been
    previously written to, doesn't assign data to variables of the wrong type, etc.
    The idea is that it's stable and secure, within the environment of the CLR.

    By default, C#.NET and VB.NET both produce verifiably safe code. There
    is a tool in Microsoft Visual Studio .NET called PEVerify that checks code is
    verifiably safe. It is possible to produce code that's not verfiably safe by
    using managed C++ or using the 'unsafe' keyword in C#. However, if that code
    attempts an unsafe operation when running it will throw a VerifierException and
    avert attempts at compromising security, etc.

    Managed code is code that is written to target the services
    of the common language runtime (see What is the Common Language Runtime?). In
    order to target these services, the code must provide a minimum level of
    information (metadata) to the runtime. All C#, Visual Basic .NET, and
    JScript .NET code is managed by default. Visual Studio .NET C++ code
    is not managed by default, but the compiler can produce managed code by
    specifying a command-line switch (/CLR).

    I would like to add details on managed data. Closely related to managed
    code is managed data—data that is allocated and de-allocated by the common
    language runtime's garbage collector. C#, Visual Basic, and JScript .NET data is
    managed by default. C# data can, however, be marked as unmanaged through the use
    of special keywords.

    Visual Studio .NET C++ data is unmanaged by default
    (even when using the /CLR switch), but when using Managed Extensions for C++, a
    class can be marked as managed by using the __gc keyword. As the name suggests,
    this means that the memory for instances of the class is managed by the garbage
    collector. In addition, the class becomes a full participating member of the
    .NET Framework community, with the benefits and restrictions that brings. An
    example of a benefit is proper interoperability with classes written in other
    languages (for example, a managed C++ class can inherit from a Visual Basic
    class). An example of a restriction is that a managed class can only inherit
    from one base class.

  • #281031
    Managed Code..
    1.The Code is Understandable by CLR.
    2.It is executed under the instructions of CLR.
    3.The code which is done by .net compatible language will be compiled two times.In the first compilation, the compiler will generate MSIL code.Which is known as Managed Code.

    UnManaged Code..
    1.The CLR cannot able to understand the code.
    2.The CLR cannot instruct the code.
    3.The second time compilation is unmanaged code.It is understood only by the machine not by the user.

    Sridhar R
    Nothing is illegal, Until You Get Caught

  • This thread is locked for new responses. Please post your comments and questions as a separate thread.
    If required, refer to the URL of this page in your new post.