locked
what is difference between managed code and un-managed code? RRS feed

  • Question

  • i want to know about difference between managed code and un managed code?
    • Edited by rakesh66 Wednesday, December 30, 2015 7:10 AM
    Wednesday, December 30, 2015 7:09 AM

Answers

  • in a nutshell ,manage what?

    it's memory.

    compare to c++ language..

    if you use new key word to Allocate a memory space.. after the executing process which use the memory is finish, you must release the memory space by yourself.

    but for managed code, this behavior has key different point.

    you use new key word to request CLR (common language runtime) help you allocate a memory space..

    THEM CLR allocate a memory space and wrap the space address to Reference object And return it to you.

    SO. reference object could store some information help CLR to decide HOW and when the memory space it refer could be release or not...And the guy in charge of the matter we call it Garbage Collector


    DON'T TRY SO HARD,THE BEST THINGS COME WHEN YOU LEAST EXPECT THEM TO.

    • Proposed as answer by Kristin Xie Thursday, December 31, 2015 5:51 AM
    • Marked as answer by Kristin Xie Wednesday, January 6, 2016 1:35 AM
    Wednesday, December 30, 2015 7:31 AM
  • Managed code is runs on the CLR (Common Language Runtime) that offers services like garbage collection, run-time type checking etc.....so we can say code is managed by CLR.

    Unmanaged code compile to machine code.

    • Marked as answer by Kristin Xie Wednesday, January 6, 2016 1:35 AM
    Wednesday, December 30, 2015 7:30 AM
  • Managed code runs under the context of the .NET CLR runtime aka NET code.

    https://msdn.microsoft.com/en-us/library/8bs2ecf4%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

    https://msdn.microsoft.com/en-us/library/windows/desktop/bb318664(v=vs.85).aspx

    Unmanaged code does not run under the context of the .NET CLR, like COM based code, which was the technology used by MS for software development prior to .NET. COM solutions are developed by VB6 and C++6 COM based development solutions as an example.

    COM solutions can be used in .NET solutions, like ActiveX and other such COM based DLL(s). But COM solutions do not run under the context of the .NET CLR, and therefore, they are unmanaged solutions running with a .NET CLR managed solution.

    https://www.microsoft.com/com/default.mspx 

    https://msdn.microsoft.com/en-us/library/aa645712(v=vs.71).aspx

    <copied>

    This section introduces two tutorials on interoperability between managed code and COM components.

    <end>

    • Proposed as answer by Kristin Xie Thursday, December 31, 2015 5:51 AM
    • Marked as answer by Kristin Xie Wednesday, January 6, 2016 1:35 AM
    Wednesday, December 30, 2015 8:04 AM
  • Hi rakesh66,

    Managed Code:

    Here code executed by CLR (Common Language Runtime). That means code need to follow some rules. So the code that targets .NET framework are managed code.

    Here runtime provides services like garbage collection, type checking, error handling. We can see this type of services in .NET framework.

    Here code is compiled by compiler into Intermediated Language. Not to machine code that could run directly on your computer.

    When you try to run managed code in a PC that PC has to have managed environment. Example, In order to run C# code you need a >NET framework install on that PC.

    Unmanaged Code:

    Here code executed by operating system.

    Here runtime does not provides services like garbage collection, type checking, error handling. So in earlier language like C/ C++ these are handeled by the programmer.

    Here code is compiled into native code. That ran on the machine where you compiled it.

    As code compiled into native code you can run your code on any PC having the same operating system.

    Hope this helps you.

    Thanks,

    Sabah Shariq




    • Edited by Sabah ShariqMVP Wednesday, December 30, 2015 9:14 AM
    • Proposed as answer by Kristin Xie Thursday, December 31, 2015 5:51 AM
    • Marked as answer by Kristin Xie Wednesday, January 6, 2016 1:35 AM
    Wednesday, December 30, 2015 9:05 AM
    • Proposed as answer by Kristin Xie Thursday, December 31, 2015 5:51 AM
    • Marked as answer by Kristin Xie Wednesday, January 6, 2016 1:35 AM
    Wednesday, December 30, 2015 9:15 AM
  • Maybe i need to add several points.
    First. manage code concept come from runtime..but not from design time...
    there are have some phase from coding to running.
    1.
    we write code use c# , vb.net,F#,...and so on which meet the CLT specification
    2.
    we use relevant language compiler to compile the code to IL code and combine (Link) them into PE file.
    3.
    We execute the PE file, the PE would setup CLR which would load the IL code And compile them to Native code (machine code) in memory use JIT Compiling if need...

    just the memory allocation part (new key word statement) of compiled native code is special to unmanaged code....

    i make a sample use pseudo-code..

    i assume follows code

    MyClass _cls = new MyClass();

    if above is unmanaged code, JIT maybe translate it to

    void* _cls = AllocateMemory(sizeof(MyClass));

    if the managed code, JIT maybe translate it to 

    ObjectRef _cls = CLR.CreateObjectRef(typeof(MyClass));

    Take a look, unmanaged just return a simple pointer to object...system could not known who and how it be used, only programmer own do known, they must to release the allocated memory by themself

    but ObjectRef is a wraper object for pointer.. it has some more information about refer than pointer...(of course , i think that is another issue need to discuss).

    So. the CLR could managed the memory through by ObjectRef, and know how to release the allocated memory.

    In a word...Programmer delegate the CLR help to manage the memory (allocation, release, moving,defragment..and so on)

    BY THE WAY
    Of course, in gereral, it impossible to has two semantics of new key word in the same language..just like in c#. new key word only represent managed code which delegate CLR to help Allocate memomery.

    Like CLR C++, introduce a gcnew key word represent managed code (instruction)... native new key word represent unmanaged code (instruction)


    DON'T TRY SO HARD,THE BEST THINGS COME WHEN YOU LEAST EXPECT THEM TO.

    • Proposed as answer by Kristin Xie Thursday, December 31, 2015 5:51 AM
    • Marked as answer by Kristin Xie Wednesday, January 6, 2016 1:35 AM
    • Edited by Matthew LEAN . D Wednesday, January 6, 2016 2:09 AM misspell something
    Wednesday, December 30, 2015 9:41 AM
  • This question is entirely too large to discuss in the forums and it is already covered in MSDN and many, many blog posts.  Please google for the answers to this question rather than posting a generic question like this in the forums.
    • Proposed as answer by Kristin Xie Thursday, December 31, 2015 5:52 AM
    • Marked as answer by Kristin Xie Wednesday, January 6, 2016 1:35 AM
    Wednesday, December 30, 2015 3:04 PM

All replies

  • Managed code is runs on the CLR (Common Language Runtime) that offers services like garbage collection, run-time type checking etc.....so we can say code is managed by CLR.

    Unmanaged code compile to machine code.

    • Marked as answer by Kristin Xie Wednesday, January 6, 2016 1:35 AM
    Wednesday, December 30, 2015 7:30 AM
  • in a nutshell ,manage what?

    it's memory.

    compare to c++ language..

    if you use new key word to Allocate a memory space.. after the executing process which use the memory is finish, you must release the memory space by yourself.

    but for managed code, this behavior has key different point.

    you use new key word to request CLR (common language runtime) help you allocate a memory space..

    THEM CLR allocate a memory space and wrap the space address to Reference object And return it to you.

    SO. reference object could store some information help CLR to decide HOW and when the memory space it refer could be release or not...And the guy in charge of the matter we call it Garbage Collector


    DON'T TRY SO HARD,THE BEST THINGS COME WHEN YOU LEAST EXPECT THEM TO.

    • Proposed as answer by Kristin Xie Thursday, December 31, 2015 5:51 AM
    • Marked as answer by Kristin Xie Wednesday, January 6, 2016 1:35 AM
    Wednesday, December 30, 2015 7:31 AM
  • Managed code runs under the context of the .NET CLR runtime aka NET code.

    https://msdn.microsoft.com/en-us/library/8bs2ecf4%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

    https://msdn.microsoft.com/en-us/library/windows/desktop/bb318664(v=vs.85).aspx

    Unmanaged code does not run under the context of the .NET CLR, like COM based code, which was the technology used by MS for software development prior to .NET. COM solutions are developed by VB6 and C++6 COM based development solutions as an example.

    COM solutions can be used in .NET solutions, like ActiveX and other such COM based DLL(s). But COM solutions do not run under the context of the .NET CLR, and therefore, they are unmanaged solutions running with a .NET CLR managed solution.

    https://www.microsoft.com/com/default.mspx 

    https://msdn.microsoft.com/en-us/library/aa645712(v=vs.71).aspx

    <copied>

    This section introduces two tutorials on interoperability between managed code and COM components.

    <end>

    • Proposed as answer by Kristin Xie Thursday, December 31, 2015 5:51 AM
    • Marked as answer by Kristin Xie Wednesday, January 6, 2016 1:35 AM
    Wednesday, December 30, 2015 8:04 AM
  • Hi rakesh66,

    Managed Code:

    Here code executed by CLR (Common Language Runtime). That means code need to follow some rules. So the code that targets .NET framework are managed code.

    Here runtime provides services like garbage collection, type checking, error handling. We can see this type of services in .NET framework.

    Here code is compiled by compiler into Intermediated Language. Not to machine code that could run directly on your computer.

    When you try to run managed code in a PC that PC has to have managed environment. Example, In order to run C# code you need a >NET framework install on that PC.

    Unmanaged Code:

    Here code executed by operating system.

    Here runtime does not provides services like garbage collection, type checking, error handling. So in earlier language like C/ C++ these are handeled by the programmer.

    Here code is compiled into native code. That ran on the machine where you compiled it.

    As code compiled into native code you can run your code on any PC having the same operating system.

    Hope this helps you.

    Thanks,

    Sabah Shariq




    • Edited by Sabah ShariqMVP Wednesday, December 30, 2015 9:14 AM
    • Proposed as answer by Kristin Xie Thursday, December 31, 2015 5:51 AM
    • Marked as answer by Kristin Xie Wednesday, January 6, 2016 1:35 AM
    Wednesday, December 30, 2015 9:05 AM
    • Proposed as answer by Kristin Xie Thursday, December 31, 2015 5:51 AM
    • Marked as answer by Kristin Xie Wednesday, January 6, 2016 1:35 AM
    Wednesday, December 30, 2015 9:15 AM
  • Maybe i need to add several points.
    First. manage code concept come from runtime..but not from design time...
    there are have some phase from coding to running.
    1.
    we write code use c# , vb.net,F#,...and so on which meet the CLT specification
    2.
    we use relevant language compiler to compile the code to IL code and combine (Link) them into PE file.
    3.
    We execute the PE file, the PE would setup CLR which would load the IL code And compile them to Native code (machine code) in memory use JIT Compiling if need...

    just the memory allocation part (new key word statement) of compiled native code is special to unmanaged code....

    i make a sample use pseudo-code..

    i assume follows code

    MyClass _cls = new MyClass();

    if above is unmanaged code, JIT maybe translate it to

    void* _cls = AllocateMemory(sizeof(MyClass));

    if the managed code, JIT maybe translate it to 

    ObjectRef _cls = CLR.CreateObjectRef(typeof(MyClass));

    Take a look, unmanaged just return a simple pointer to object...system could not known who and how it be used, only programmer own do known, they must to release the allocated memory by themself

    but ObjectRef is a wraper object for pointer.. it has some more information about refer than pointer...(of course , i think that is another issue need to discuss).

    So. the CLR could managed the memory through by ObjectRef, and know how to release the allocated memory.

    In a word...Programmer delegate the CLR help to manage the memory (allocation, release, moving,defragment..and so on)

    BY THE WAY
    Of course, in gereral, it impossible to has two semantics of new key word in the same language..just like in c#. new key word only represent managed code which delegate CLR to help Allocate memomery.

    Like CLR C++, introduce a gcnew key word represent managed code (instruction)... native new key word represent unmanaged code (instruction)


    DON'T TRY SO HARD,THE BEST THINGS COME WHEN YOU LEAST EXPECT THEM TO.

    • Proposed as answer by Kristin Xie Thursday, December 31, 2015 5:51 AM
    • Marked as answer by Kristin Xie Wednesday, January 6, 2016 1:35 AM
    • Edited by Matthew LEAN . D Wednesday, January 6, 2016 2:09 AM misspell something
    Wednesday, December 30, 2015 9:41 AM
  • This question is entirely too large to discuss in the forums and it is already covered in MSDN and many, many blog posts.  Please google for the answers to this question rather than posting a generic question like this in the forums.
    • Proposed as answer by Kristin Xie Thursday, December 31, 2015 5:52 AM
    • Marked as answer by Kristin Xie Wednesday, January 6, 2016 1:35 AM
    Wednesday, December 30, 2015 3:04 PM